Page 1 of 1

Managing CEGUI objects with shared_ptr (C++11)

Posted: Mon Dec 07, 2015 11:06
by vordhosbn
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.

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

Posted: Mon Dec 07, 2015 11:14
by lucebac
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).

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

Posted: Mon Dec 07, 2015 14:25
by vordhosbn
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.

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

Posted: Mon Dec 07, 2015 14:31
by lucebac
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.

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

Posted: Mon Dec 07, 2015 15:23
by YaronCT
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.

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

Posted: Tue Dec 08, 2015 09:05
by vordhosbn
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.