Managing CEGUI objects with shared_ptr (C++11)

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

vordhosbn
Just popping in
Just popping in
Posts: 8
Joined: Sat Sep 05, 2015 18:27

Managing CEGUI objects with shared_ptr (C++11)

Postby vordhosbn » Mon Dec 07, 2015 11:06

Hi,

I am trying to use shared_ptr to control the lifetime of some CEGUI objects (Renderer,System, ResourceProvider).

Here I am constucting a shared_ptr for the System object:

Code: Select all

    ceguiSystem = std::shared_ptr<CEGUI::System>(
        &CEGUI::System::create(*openGLRenderer, resourceProvider.get()), //get the raw pointer here
        CEGUI::System::destroy // use this custom deleter
        );
       


Which works fine.

But for the renderer classes, not so much.
I have to use a lambda expression, because I have to call the destroy() method with a parameter:

Code: Select all

    openGLRenderer = std::shared_ptr<CEGUI::OpenGLRenderer>(
        &(CEGUI::OpenGLRenderer::create()), // get raw pointer here
        [](CEGUI::OpenGLRenderer * ptr) // lambda expression
    {
        CEGUI::OpenGLRenderer::destroy(*ptr); // call the custom deleter with argument the object
    }
    );

(fails to compile with error C2197: 'void (__cdecl *)(void)': too many arguments for call)

The syntax should be fine - take this self contained example:

Code: Select all

#include <memory>

class SomeClass
{
public:
    static void destroy(SomeClass& a) {}
};

std::shared_ptr<SomeClass> sPtr;


int main()
{
    SomeClass a;
    sPtr = std::shared_ptr<SomeClass>(
        &a,
        [](SomeClass* a)
    {
        SomeClass::destroy(*a);
    }
    );

    return 0;
}


I can't find any differences between the two, but one of them is not compiling.

lucebac
Just can't stay away
Just can't stay away
Posts: 193
Joined: Sat May 24, 2014 21:55

Re: Managing CEGUI objects with shared_ptr (C++11)

Postby lucebac » Mon Dec 07, 2015 11:14

Why do you need shared_ptr? CEGUI creates and uses only one instance of CEGUI:System (and others) internally with singeltons. Each time you call CEGUI::System::getSingletonPtr() it will return the same pointer (if you created CEGUI::System before, of course).

vordhosbn
Just popping in
Just popping in
Posts: 8
Joined: Sat Sep 05, 2015 18:27

Re: Managing CEGUI objects with shared_ptr (C++11)

Postby vordhosbn » Mon Dec 07, 2015 14:25

But I need to explicitly delete those objects using destroy() method, right?

Sure I can put manually call destroy() in a destructor of a wrapper class or something similar. but shared_ptr seemed just right for this. And what irritates me is that custom deleter works with short sample code and not with CEGUI classes and I can't seem to figure out why.

lucebac
Just can't stay away
Just can't stay away
Posts: 193
Joined: Sat May 24, 2014 21:55

Re: Managing CEGUI objects with shared_ptr (C++11)

Postby lucebac » Mon Dec 07, 2015 14:31

You may want to have a look into lines 323 through 325: https://bitbucket.org/lucebac/cegui/src ... L2.cpp-322
This is the proper way to shut CEGUI down. Of course, if you have, say, OgreRenderer or any other renderer, you need to call OgreRenderer::destroy(*renderer) and so on.

YaronCT
CEGUI Team
Posts: 448
Joined: Fri Jun 19, 2015 12:18
Location: Kiryat-Bialik, Israel

Re: Managing CEGUI objects with shared_ptr (C++11)

Postby YaronCT » Mon Dec 07, 2015 15:23

vordhosbn: I've just tried to compile your code and it compiled successfully, so I'm afraid I can't help u :(

I'm using GCC 4.9.2 on Linux x64.

vordhosbn
Just popping in
Just popping in
Posts: 8
Joined: Sat Sep 05, 2015 18:27

Re: Managing CEGUI objects with shared_ptr (C++11)

Postby vordhosbn » Tue Dec 08, 2015 09:05

I am using MS C++ 19.00.23026, but it would be strange if its compiler dependant... I would guess its something simple, but well hidden due to the cryptic template error messages. :D
Anyways, thanks for the help.

I ended up wrapping the whole CEGUI system object managemenet in its own class.


Return to “Help”

Who is online

Users browsing this forum: No registered users and 12 guests