Remove Luabind ownership on C++ pointer?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Remove Luabind ownership on C++ pointer?

golgoth

Greetings,

 

I’m wondering how to remove Luabind ownership on C++ pointers:

 

C++

module(lua)

[

                    class_<Object>("Object")

                    .def(constructor<>())

];

 

.lua

local object = Object();

 

 

I would like Luabind NOT to delete the object C++ pointer when lua_gc() is called.

 

If anyone found an elegant solution for this, I’m definitely interested to hear about it.

 

Thx,

 

 


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

eduard mueller
Hold the object in a boost::shared_ptr or some other reference
counting smart_ptr in Lua:
class_<Object, boost::shared_ptr<Object> >("Object")

Then the object will be kept alive as long as another shared_ptr uses
the object in C++. Aka, the instance that is created in Lua will only
release the shared pointer, not destruct the object itself when
garbage collected.

Best regards,
Eduard


On Thu, Sep 30, 2010 at 05:16, neosettlers <[hidden email]> wrote:

> Greetings,
>
>
>
> I’m wondering how to remove Luabind ownership on C++ pointers:
>
>
>
> C++
>
> module(lua)
>
> [
>
>                     class_<Object>("Object")
>
>                     .def(constructor<>())
>
> ];
>
>
>
> .lua
>
> local object = Object();
>
>
>
> …
>
>
>
> I would like Luabind NOT to delete the object C++ pointer when lua_gc() is
> called.
>
>
>
> If anyone found an elegant solution for this, I’m definitely interested to
> hear about it.
>
>
>
> Thx,
>
>
>
>
>
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

Iliya Trendafilov
Another option is to register a factory function instead of constructor. E.g.

C++
Object* CreateObject() { return new Object(); }

module(lua)

[

                    class_<Object>("Object")

                    .def("CreateObject", &CreateObject)

];

 

Lua:

local object = CreateObject();


You'll be solely responsible for deleting these objects in that case.


Another thing I did once is to keep a Lua table with all created instances. In Lua:


function CreateObject(...)

      objects = objects or {}

      local object = Object()

      table.insert(objects, object)

      return object

end


and then create all your instances through that function.


I'll leave you to decide which of the methods is the most elegant :)


On Thu, Sep 30, 2010 at 1:30 PM, eduard mueller <[hidden email]> wrote:
Hold the object in a boost::shared_ptr or some other reference
counting smart_ptr in Lua:
class_<Object, boost::shared_ptr<Object> >("Object")

Then the object will be kept alive as long as another shared_ptr uses
the object in C++. Aka, the instance that is created in Lua will only
release the shared pointer, not destruct the object itself when
garbage collected.

Best regards,
Eduard


On Thu, Sep 30, 2010 at 05:16, neosettlers <[hidden email]> wrote:
> Greetings,
>
>
>
> I’m wondering how to remove Luabind ownership on C++ pointers:
>
>
>
> C++
>
> module(lua)
>
> [
>
>                     class_<Object>("Object")
>
>                     .def(constructor<>())
>
> ];
>
>
>
> .lua
>
> local object = Object();
>
>
>
> …
>
>
>
> I would like Luabind NOT to delete the object C++ pointer when lua_gc() is
> called.
>
>
>
> If anyone found an elegant solution for this, I’m definitely interested to
> hear about it.
>
>
>
> Thx,
>
>
>
>
>
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

Nigel Atkinson-2
A factory function works well, and also works well for singleton classes
or other times where you need more control over creation and deletion.

In order to use them however, you have to bind them as a static (either
free or a method), so that you don't need a existing object to call it
on.

e.g
// Free style
module(lua)
[
        class_<Object>("Object"),
        def("CreateObject", &CreateObject)
];

// Method style
module(lua)
[
        class_<Object>("Object")
        .scope
        [
                def("Create", &CreateObject)
        ]
];

obj = CreateObject() // Free function

obj = Object.Create() // Static method, notice the '.'



On Thu, 2010-09-30 at 14:26 +0200, Iliya Trendafilov wrote:

> Another option is to register a factory function instead of
> constructor. E.g.
>
> C++
> Object* CreateObject() { return new Object(); }
>
>
> module(lua)
>
> [
>
>                     class_<Object>("Object")
>
>                     .def("CreateObject", &CreateObject)
>
>
> ];
>
>  
>
> Lua:
>
> local object = CreateObject();
>
>
> You'll be solely responsible for deleting these objects in that case.
>
>
> Another thing I did once is to keep a Lua table with all created
> instances. In Lua:
>
>
> function CreateObject(...)
>
>       objects = objects or {}
>
>
>       local object = Object()
>
>       table.insert(objects, object)
>
>
>       return object
>
>
> end
>
>
> and then create all your instances through that function.
>
>
> I'll leave you to decide which of the methods is the most elegant :)
>
>
>
> On Thu, Sep 30, 2010 at 1:30 PM, eduard mueller
> <[hidden email]> wrote:
>         Hold the object in a boost::shared_ptr or some other reference
>         counting smart_ptr in Lua:
>         class_<Object, boost::shared_ptr<Object> >("Object")
>        
>         Then the object will be kept alive as long as another
>         shared_ptr uses
>         the object in C++. Aka, the instance that is created in Lua
>         will only
>         release the shared pointer, not destruct the object itself
>         when
>         garbage collected.
>        
>         Best regards,
>         Eduard
>        
>        
>        
>         On Thu, Sep 30, 2010 at 05:16, neosettlers
>         <[hidden email]> wrote:
>         > Greetings,
>         >
>         >
>         >
>         > I’m wondering how to remove Luabind ownership on C++
>         pointers:
>         >
>         >
>         >
>         > C++
>         >
>         > module(lua)
>         >
>         > [
>         >
>         >                     class_<Object>("Object")
>         >
>         >                     .def(constructor<>())
>         >
>         > ];
>         >
>         >
>         >
>         > .lua
>         >
>         > local object = Object();
>         >
>         >
>         >
>         > …
>         >
>         >
>         >
>         > I would like Luabind NOT to delete the object C++ pointer
>         when lua_gc() is
>         > called.
>         >
>         >
>         >
>         > If anyone found an elegant solution for this, I’m definitely
>         interested to
>         > hear about it.
>         >
>         >
>         >
>         > Thx,
>         >
>         >
>         >
>         >
>         >
>        
>         >
>         ------------------------------------------------------------------------------
>         > Start uncovering the many advantages of virtual appliances
>         > and start using them to simplify application deployment and
>         > accelerate your shift to cloud computing.
>         > http://p.sf.net/sfu/novell-sfdev2dev
>         > _______________________________________________
>         > luabind-user mailing list
>         > [hidden email]
>         > https://lists.sourceforge.net/lists/listinfo/luabind-user
>         >
>         >
>        
>         ------------------------------------------------------------------------------
>         Start uncovering the many advantages of virtual appliances
>         and start using them to simplify application deployment and
>         accelerate your shift to cloud computing.
>         http://p.sf.net/sfu/novell-sfdev2dev
>         _______________________________________________
>         luabind-user mailing list
>         [hidden email]
>         https://lists.sourceforge.net/lists/listinfo/luabind-user
>
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

golgoth
In reply to this post by eduard mueller
Greetings,

Just to let you know my situation, I went from VS 2008, XP 64, luabind 0.9 rc to Windows 7 64, VS 2010, luabind 0.9 and the latest boost lib ...!

Before the Pangaea, I was doing this to prevent deleting and it work like a charm:

Technique "Override"

#define LuaClassNoDelete(in_class)\
template <> struct delete_s<in_class> {static void apply(void* ptr) {}};\
template <> struct destruct_only_s<in_class> {static void apply(void* ptr){}};

LuaClassNoDelete(Object)

Unfortunately,  this technique does not working anymore since those struct seem to be obsolete somehow... I have no clue why. I'm sensing one of those packages I've updated too is doing Hocus Pocus stuff.

Technique "shared_ptr"

I like this method but so far, I can’t make it work. Even when using shared_ptr its still go there on GC:

cheked_delete.hpp

template<class T> inline void checked_delete(T * x)
{
    // intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete x; <<<<< CRASH here since the object has previously been deleted.
}


Any help on either one of those would be extremely valuable.

Thx again,

-----Original Message-----
From: eduard mueller [mailto:[hidden email]]
Sent: Thursday, September 30, 2010 7:31 AM
To: [hidden email]
Subject: Re: [luabind] Remove Luabind ownership on C++ pointer?

Hold the object in a boost::shared_ptr or some other reference counting smart_ptr in Lua:
class_<Object, boost::shared_ptr<Object> >("Object")

Then the object will be kept alive as long as another shared_ptr uses the object in C++. Aka, the instance that is created in Lua will only release the shared pointer, not destruct the object itself when garbage collected.

Best regards,
Eduard


On Thu, Sep 30, 2010 at 05:16, neosettlers <[hidden email]> wrote:

> Greetings,
>
>
>
> I’m wondering how to remove Luabind ownership on C++ pointers:
>
>
>
> C++
>
> module(lua)
>
> [
>
>                     class_<Object>("Object")
>
>                     .def(constructor<>())
>
> ];
>
>
>
> .lua
>
> local object = Object();
>
>
>
> …
>
>
>
> I would like Luabind NOT to delete the object C++ pointer when
> lua_gc() is called.
>
>
>
> If anyone found an elegant solution for this, I’m definitely
> interested to hear about it.
>
>
>
> Thx,
>
>
>
>
>
> ----------------------------------------------------------------------
> -------- Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and accelerate
> your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances and start using them to simplify application deployment and accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

Iliya Trendafilov
In reply to this post by Nigel Atkinson-2
Yeah, my bad, free function is what I meant. Thanks for the correction :)

On Thu, Sep 30, 2010 at 3:06 PM, Nigel Atkinson <[hidden email]> wrote:
A factory function works well, and also works well for singleton classes
or other times where you need more control over creation and deletion.

In order to use them however, you have to bind them as a static (either
free or a method), so that you don't need a existing object to call it
on.

e.g
// Free style
module(lua)
[
       class_<Object>("Object"),
       def("CreateObject", &CreateObject)
];

// Method style
module(lua)
[
       class_<Object>("Object")
       .scope
       [
               def("Create", &CreateObject)
       ]
];

obj = CreateObject()    // Free function

obj = Object.Create()   // Static method, notice the '.'



On Thu, 2010-09-30 at 14:26 +0200, Iliya Trendafilov wrote:
> Another option is to register a factory function instead of
> constructor. E.g.
>
> C++
> Object* CreateObject() { return new Object(); }
>
>
> module(lua)
>
> [
>
>                     class_<Object>("Object")
>
>                     .def("CreateObject", &CreateObject)
>
>
> ];
>
>
>
> Lua:
>
> local object = CreateObject();
>
>
> You'll be solely responsible for deleting these objects in that case.
>
>
> Another thing I did once is to keep a Lua table with all created
> instances. In Lua:
>
>
> function CreateObject(...)
>
>       objects = objects or {}
>
>
>       local object = Object()
>
>       table.insert(objects, object)
>
>
>       return object
>
>
> end
>
>
> and then create all your instances through that function.
>
>
> I'll leave you to decide which of the methods is the most elegant :)
>
>
>
> On Thu, Sep 30, 2010 at 1:30 PM, eduard mueller
> <[hidden email]> wrote:
>         Hold the object in a boost::shared_ptr or some other reference
>         counting smart_ptr in Lua:
>         class_<Object, boost::shared_ptr<Object> >("Object")
>
>         Then the object will be kept alive as long as another
>         shared_ptr uses
>         the object in C++. Aka, the instance that is created in Lua
>         will only
>         release the shared pointer, not destruct the object itself
>         when
>         garbage collected.
>
>         Best regards,
>         Eduard
>
>
>
>         On Thu, Sep 30, 2010 at 05:16, neosettlers
>         <[hidden email]> wrote:
>         > Greetings,
>         >
>         >
>         >
>         > I’m wondering how to remove Luabind ownership on C++
>         pointers:
>         >
>         >
>         >
>         > C++
>         >
>         > module(lua)
>         >
>         > [
>         >
>         >                     class_<Object>("Object")
>         >
>         >                     .def(constructor<>())
>         >
>         > ];
>         >
>         >
>         >
>         > .lua
>         >
>         > local object = Object();
>         >
>         >
>         >
>         > …
>         >
>         >
>         >
>         > I would like Luabind NOT to delete the object C++ pointer
>         when lua_gc() is
>         > called.
>         >
>         >
>         >
>         > If anyone found an elegant solution for this, I’m definitely
>         interested to
>         > hear about it.
>         >
>         >
>         >
>         > Thx,
>         >
>         >
>         >
>         >
>         >
>
>         >
>         ------------------------------------------------------------------------------
>         > Start uncovering the many advantages of virtual appliances
>         > and start using them to simplify application deployment and
>         > accelerate your shift to cloud computing.
>         > http://p.sf.net/sfu/novell-sfdev2dev
>         > _______________________________________________
>         > luabind-user mailing list
>         > [hidden email]
>         > https://lists.sourceforge.net/lists/listinfo/luabind-user
>         >
>         >
>
>         ------------------------------------------------------------------------------
>         Start uncovering the many advantages of virtual appliances
>         and start using them to simplify application deployment and
>         accelerate your shift to cloud computing.
>         http://p.sf.net/sfu/novell-sfdev2dev
>         _______________________________________________
>         luabind-user mailing list
>         [hidden email]
>         https://lists.sourceforge.net/lists/listinfo/luabind-user
>
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________ luabind-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

eduard mueller
In reply to this post by golgoth
On Thu, Sep 30, 2010 at 16:56, neosettlers <[hidden email]> wrote:
> Technique "shared_ptr"
>
> I like this method but so far, I can’t make it work. Even when using shared_ptr its still go there on GC:

When using shared_ptrs, you have to use them everywhere to
transfer/keep track of the objects lifetime correctly. Using something
which stores the reference count in the object itself, like
boost::intrusive_ptr may be the better choice then. But this really
depends on what you actually do with the object...

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

golgoth
Hummm...

Luabind, you’re a guest in my home (C++ library), on your way out, please do not take my garbage with you! (I wish I wouldn’t have to ask)

No offense but making extra functions or even worst, a manager for this is embarrassing... this should be solved with something like this:

.def(constructor<>())
.def(destructor<>()) /// if present, take the garbage out, if not please, mind your own business!

Regards,

-----Original Message-----
From: eduard mueller [mailto:[hidden email]]
Sent: Thursday, September 30, 2010 11:08 AM
To: [hidden email]
Subject: Re: [luabind] Remove Luabind ownership on C++ pointer?

On Thu, Sep 30, 2010 at 16:56, neosettlers <[hidden email]> wrote:
> Technique "shared_ptr"
>
> I like this method but so far, I can’t make it work. Even when using shared_ptr its still go there on GC:

When using shared_ptrs, you have to use them everywhere to transfer/keep track of the objects lifetime correctly. Using something which stores the reference count in the object itself, like boost::intrusive_ptr may be the better choice then. But this really depends on what you actually do with the object...

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances and start using them to simplify application deployment and accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

Daniel Wallin
On Thu, Sep 30, 2010 at 5:43 PM, neosettlers <[hidden email]> wrote:
> Hummm...
>
> Luabind, you’re a guest in my home (C++ library), on your way out, please do not take my garbage with you! (I wish I wouldn’t have to ask)
>
> No offense but making extra functions or even worst, a manager for this is embarrassing... this should be solved with something like this:
>
> .def(constructor<>())
> .def(destructor<>()) /// if present, take the garbage out, if not please, mind your own business!

Well, this is a very rare request. Having to explicitly state that
instances should be destroyed would screw over almost everyone.
Basically, you have some class whose instances somehow manages their
own lifetime? You can try holding the Lua-created instances in a
special smart pointer that doesn't delete:

  template <class T>
  struct dummy_ptr
  {
      dummy_ptr(T* p_) : p(p_) {}
      T* p;
  };

  template <class T>
  T* get_pointer(dummy_ptr<T> const& p)
  {
      return p.p;
  }

  ...

  class_<Whatever, dummy_ptr<Whatever> >("Whatever")
      .def(constructor<>(...))

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

golgoth
Thx everyone for the inputs,

Well, this is a very rare request. Having to explicitly state that instances
should be destroyed would screw over almost everyone.

I think it is at least practical to have the choice to managed your memory
yourself without reinventing the wheel.

Basically, you have some class whose instances somehow manages their own
lifetime?

Of course, all classes are registered and unregister by an object manager.

You can try holding the Lua-created instances in a special smart pointer
that doesn't delete:

I'm wondering why would you give so much power to scripting... In my case,
scripting is used as an interface to access the C++  functionalities… not to
take over!

I managed to solved my problem in modifying the source directly:

object_rep::~object_rep()
{
        if (!m_instance)
            return;
 ///    m_instance->~instance_holder();
        deallocate(m_instance);
}
 
And voila!

-----Original Message-----
From: Daniel Wallin [mailto:[hidden email]]
Sent: Thursday, September 30, 2010 3:02 PM
To: [hidden email]
Subject: Re: [luabind] Remove Luabind ownership on C++ pointer?

On Thu, Sep 30, 2010 at 5:43 PM, neosettlers <[hidden email]>
wrote:
> Hummm...
>
> Luabind, you’re a guest in my home (C++ library), on your way out,
> please do not take my garbage with you! (I wish I wouldn’t have to
> ask)
>
> No offense but making extra functions or even worst, a manager for this is
embarrassing... this should be solved with something like this:
>
> .def(constructor<>())
> .def(destructor<>()) /// if present, take the garbage out, if not please,
mind your own business!

Well, this is a very rare request. Having to explicitly state that instances
should be destroyed would screw over almost everyone.
Basically, you have some class whose instances somehow manages their own
lifetime? You can try holding the Lua-created instances in a special smart
pointer that doesn't delete:

  template <class T>
  struct dummy_ptr
  {
      dummy_ptr(T* p_) : p(p_) {}
      T* p;
  };

  template <class T>
  T* get_pointer(dummy_ptr<T> const& p)
  {
      return p.p;
  }

  ...

  class_<Whatever, dummy_ptr<Whatever> >("Whatever")
      .def(constructor<>(...))

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

----------------------------------------------------------------------------
--
Start uncovering the many advantages of virtual appliances and start using
them to simplify application deployment and accelerate your shift to cloud
computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

Daniel Wallin
On Fri, Oct 1, 2010 at 12:15 AM, neosettlers <[hidden email]> wrote:
> Thx everyone for the inputs,
>
>> Well, this is a very rare request. Having to explicitly state that instances
>> should be destroyed would screw over almost everyone.
>
> I think it is at least practical to have the choice to managed your memory
> yourself without reinventing the wheel.

Sure, and you do have that choice.

>> Basically, you have some class whose instances somehow manages their own
>> lifetime?
>
> Of course, all classes are registered and unregister by an object manager.

Right. Typically this would be done by either:

  1. Creating the object in lua, and then have some function on the manager
      that explicitly takes ownership, or...

  2. Having a factory function on your manager that creates the object and
      returns non owning pointer.

(2) works automatically, and luabind has adopt() for (1). Perhaps we
should have a release_ownership() policy similar to adopt(), that can
be used post-construction, I'm not sure. That said, I believe your
problem can be handled as I show below.

> I managed to solved my problem in modifying the source directly:
>
> object_rep::~object_rep()
> {
>        if (!m_instance)
>            return;
>  ///    m_instance->~instance_holder();
>        deallocate(m_instance);
> }

Did you try what I posted? In fact, it can be done in an even simpler way:

  class_<Whatever, Whatever*>("Whatever")

Makes luabind hold the newly created instance in a raw Whatever*, and
thus won't delete it.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Remove Luabind ownership on C++ pointer?

golgoth
  class_<Whatever, Whatever*>("Whatever")

Makes luabind hold the newly created instance in a raw Whatever*, and thus
won't delete it.

Awesome, even better! Is this mentioned anywhere in the doc?

Thank you.

-----Original Message-----
From: Daniel Wallin [mailto:[hidden email]]
Sent: Friday, October 01, 2010 5:28 AM
To: [hidden email]
Subject: Re: [luabind] Remove Luabind ownership on C++ pointer?

On Fri, Oct 1, 2010 at 12:15 AM, neosettlers <[hidden email]>
wrote:
> Thx everyone for the inputs,
>
>> Well, this is a very rare request. Having to explicitly state that
>> instances should be destroyed would screw over almost everyone.
>
> I think it is at least practical to have the choice to managed your
> memory yourself without reinventing the wheel.

Sure, and you do have that choice.

>> Basically, you have some class whose instances somehow manages their
>> own lifetime?
>
> Of course, all classes are registered and unregister by an object manager.

Right. Typically this would be done by either:

  1. Creating the object in lua, and then have some function on the manager
      that explicitly takes ownership, or...

  2. Having a factory function on your manager that creates the object and
      returns non owning pointer.

(2) works automatically, and luabind has adopt() for (1). Perhaps we should
have a release_ownership() policy similar to adopt(), that can be used
post-construction, I'm not sure. That said, I believe your problem can be
handled as I show below.

> I managed to solved my problem in modifying the source directly:
>
> object_rep::~object_rep()
> {
>        if (!m_instance)
>            return;
>  ///    m_instance->~instance_holder();
>        deallocate(m_instance);
> }

Did you try what I posted? In fact, it can be done in an even simpler way:

  class_<Whatever, Whatever*>("Whatever")

Makes luabind hold the newly created instance in a raw Whatever*, and thus
won't delete it.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

----------------------------------------------------------------------------
--
Start uncovering the many advantages of virtual appliances and start using
them to simplify application deployment and accelerate your shift to cloud
computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user