[C++-sig] Object lifetime issue

Roman Yakovenko roman.yakovenko at gmail.com
Tue Sep 4 09:06:06 CEST 2007


On 9/2/07, Matt Holmes <tegan at gnometank.net> wrote:
>
> I am trying to expose one of the classes in my engine as an abstract
> class that is used as the base for a series of Python classes.
>
> That class is called InputCommand and is defined as such:
> namespace Stasis {
>         class InputCommand {
>         public:
>                 virtual bool execute() = 0;
>         };
> }
>
> To expose that class, I created the following wrapper:
>
> class InputCommandWrapper : public Stasis::InputCommand, public
> wrapper<Stasis::InputCommand> {
> public:
>         bool execute() {
>                 return this->get_override("execute")();
>         }
> };
>
> And added it to my Boost.Python module like so:
>
> class_<InputCommandWrapper, boost::noncopyable>("InputCommand")
>         .def("execute", pure_virtual(&InputCommand::execute));
>
> Everything seems okay so far, but then I have another class called
> InputManager. This class exposes a function, registerCommand, that takes
> a const std::string& and an InputCommand*. That classes partial
> defintion is:
>
> class InputManager : public Singleton<InputManager>
> public:
>         static InputManager& getSingleton() { return *ms_Singleton; }
>         static InputManager* getSingletonPtr() { return ms_Singleton; }
>
>         InputManager();
>         ~InputManager();
>
>         void executeCommand(const string& cmdName);
>         void registerCommand(const string& cmdName, InputCommand* cmd);
> }



Who is responsible for cmd lifetime, InputManager or somebody else? This
answer is a key for correct solution.

I attached 2 files that contains solution for your problem

P.S. The code is not minimal, because I extracted it from Py++ unittests

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070904/9ccbf8f9/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: transfer_ownership.cpp
Type: text/x-c++src
Size: 4836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070904/9ccbf8f9/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: transfer_ownership_to_be_exported.hpp
Type: text/x-c++hdr
Size: 690 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070904/9ccbf8f9/attachment.hpp>


More information about the Cplusplus-sig mailing list