Copy a smart pointer into allocated memory
Hi, and sorry for the probable newbieness of this question! I want to put weak_ptr into a piece of memory previously allocated. this is my code: // construct a weak_ptr from a share_ptr boost::weak_ptr<Obj>* cameraPtr = new boost::weak_ptr<Obj>(obj_share_ptr); // lua_newuserdata allocs memory and returns a pointer to it, just like malloc. boost::weak_ptr<Obj>* camera = static_cast<boost::weak_ptr<Obj>*>(lua_newuserdata(pLuaState,sizeof(boost::weak_ptr<Obj>))); Now i want camera to be a weak_ptr of ObjPtr. I tried different ways to do this, such as *camera = boost::weak_ptr<Obj>(obj_share_ptr); or camera->swap(*cameraPtr); or *camera = *cameraPtr; I think none of these work because in fact they all do a swap() between camera and cameraPtr. Since camera is just an malloc, it will try to swap dirty memory and segfault sometimes (yes, sometimes this worked). Out of desperation, i did this: memcpy(camera,cameraPtr,sizeof(boost::weak_ptr<Obj>)); Which seems to work but it stinks. It's copying an object with memcpy instead of using its constructor. Is there a better way to do this? Thanks, Tiago Alves
2009/6/12 Tiago Alves <talves@tapestry.pt>
Hi, and sorry for the probable newbieness of this question!
I want to put weak_ptr into a piece of memory previously allocated.
this is my code:
// construct a weak_ptr from a share_ptr boost::weak_ptr<Obj>* cameraPtr = new boost::weak_ptr<Obj>(obj_share_ptr); // lua_newuserdata allocs memory and returns a pointer to it, just like malloc. boost::weak_ptr<Obj>* camera = static_cast<boost::weak_ptr<Obj>*>(lua_newuserdata(pLuaState,sizeof(boost::weak_ptr<Obj>)));
Now i want camera to be a weak_ptr of ObjPtr. I tried different ways to do this, such as *camera = boost::weak_ptr<Obj>(obj_share_ptr); or camera->swap(*cameraPtr); or *camera = *cameraPtr;
Try this: new (camera) boost::weak_ptr<Obj>(*cameraPtr); Roman Perepelitsa.
AMDG Tiago Alves wrote:
Hi, and sorry for the probable newbieness of this question!
I want to put weak_ptr into a piece of memory previously allocated.
this is my code:
// construct a weak_ptr from a share_ptr boost::weak_ptr<Obj>* cameraPtr = new boost::weak_ptr<Obj>(obj_share_ptr); // lua_newuserdata allocs memory and returns a pointer to it, just like malloc. boost::weak_ptr<Obj>* camera = static_cast<boost::weak_ptr<Obj>*>(lua_newuserdata(pLuaState,sizeof(boost::weak_ptr<Obj>)));
Now i want camera to be a weak_ptr of ObjPtr. I tried different ways to do this, such as *camera = boost::weak_ptr<Obj>(obj_share_ptr); or camera->swap(*cameraPtr); or *camera = *cameraPtr;
I think none of these work because in fact they all do a swap() between camera and cameraPtr. Since camera is just an malloc, it will try to swap dirty memory and segfault sometimes (yes, sometimes this worked).
Out of desperation, i did this: memcpy(camera,cameraPtr,sizeof(boost::weak_ptr<Obj>));
Which seems to work but it stinks. It's copying an object with memcpy instead of using its constructor.
Is there a better way to do this?
Use placement new. new(camara) boost::weak_ptr<Obj>(obj_share_ptr); In Christ, Steven Watanabe
Tiago Alves:
Hi, and sorry for the probable newbieness of this question!
I want to put weak_ptr into a piece of memory previously allocated.
this is my code:
// construct a weak_ptr from a share_ptr boost::weak_ptr<Obj>* cameraPtr = new boost::weak_ptr<Obj>(obj_share_ptr); // lua_newuserdata allocs memory and returns a pointer to it, just like malloc. boost::weak_ptr<Obj>* camera = static_cast<boost::weak_ptr<Obj>*>(lua_newuserdata(pLuaState,sizeof(boost::weak_ptr<Obj>)));
Now i want camera to be a weak_ptr of ObjPtr. I tried different ways to do this, such as *camera = boost::weak_ptr<Obj>(obj_share_ptr); or camera->swap(*cameraPtr); or *camera = *cameraPtr;
I think none of these work because in fact they all do a swap() between camera and cameraPtr. Since camera is just an malloc, it will try to swap dirty memory and segfault sometimes (yes, sometimes this worked).
In C++, the way to construct an object of type X into uninitialized memory (pointed to by a pointer p) is called placement new: #include <new> new( p ) X; In your case, you'd do: void * p = lua_newuserdata( pLuaState, sizeof( boost::weak_ptr<Obj> ) ); new( p ) boost::weak_ptr<Obj>( obj_share_ptr ); The last line constructs a new weak_ptr in the memory at p from the obj_share_ptr argument (same as cameraPtr in your original example, but without the allocation.) Later (probably in your 'gc' metamethod, whatever that is :-) ) you'll also have to dispose of the weak_ptr object; this is done by invoking the destructor: p->~weak_ptr<Obj>(); In weak_ptr's case, you can also cheat and just reset() it, as its empty state consists of two NULL pointers and can be garbage-collected with impunity, but in principle, the proper way to end the lifetime of such an object is to call the destructor.
Thanks. I never used that placement new before. Always leaning... :) Also, thanks for the heads up about the destructor. I am destroying the weak_ptr() in the lua garbage collector. Tiago Alves Peter Dimov escreveu:
Tiago Alves:
Hi, and sorry for the probable newbieness of this question!
I want to put weak_ptr into a piece of memory previously allocated.
this is my code:
// construct a weak_ptr from a share_ptr boost::weak_ptr<Obj>* cameraPtr = new boost::weak_ptr<Obj>(obj_share_ptr); // lua_newuserdata allocs memory and returns a pointer to it, just like malloc. boost::weak_ptr<Obj>* camera = static_cast<boost::weak_ptr<Obj>*>(lua_newuserdata(pLuaState,sizeof(boost::weak_ptr<Obj>)));
Now i want camera to be a weak_ptr of ObjPtr. I tried different ways to do this, such as *camera = boost::weak_ptr<Obj>(obj_share_ptr); or camera->swap(*cameraPtr); or *camera = *cameraPtr;
I think none of these work because in fact they all do a swap() between camera and cameraPtr. Since camera is just an malloc, it will try to swap dirty memory and segfault sometimes (yes, sometimes this worked).
In C++, the way to construct an object of type X into uninitialized memory (pointed to by a pointer p) is called placement new:
#include <new>
new( p ) X;
In your case, you'd do:
void * p = lua_newuserdata( pLuaState, sizeof( boost::weak_ptr<Obj> ) ); new( p ) boost::weak_ptr<Obj>( obj_share_ptr );
The last line constructs a new weak_ptr in the memory at p from the obj_share_ptr argument (same as cameraPtr in your original example, but without the allocation.)
Later (probably in your 'gc' metamethod, whatever that is :-) ) you'll also have to dispose of the weak_ptr object; this is done by invoking the destructor:
p->~weak_ptr<Obj>();
In weak_ptr's case, you can also cheat and just reset() it, as its empty state consists of two NULL pointers and can be garbage-collected with impunity, but in principle, the proper way to end the lifetime of such an object is to call the destructor.
participants (4)
-
Peter Dimov
-
Roman Perepelitsa
-
Steven Watanabe
-
Tiago Alves