double deletion problem with shared pointers.
Nigel Atkinson <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <1259200363.3058.36.camel@hurin> |
The attached code shows the double deletion problem I encounted. I previously thought that the problem only surfaced when using the shared_ptr_converter. However it looks like it happens either way. Nigel ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user
main.cpp
(text/x-c++src, 1.4 KB)
#include <iostream>
#include <lua.hpp>
#include <luabind/luabind.hpp>
#include <boost/shared_ptr.hpp>
//#include <luabind/shared_ptr_converter.hpp>
#include <string>
using namespace luabind;
using namespace std;
class widget
{
public:
widget( string caption ) : name(caption)
{
cout << "widget constructed: " << this << " " << name << endl;
}
virtual ~widget()
{
cout << "widget destructed: " << this << " " << name << endl;
}
protected:
string name;
};
class widget_wrapper : public widget, public wrap_base
{
public:
widget_wrapper( string caption ) : widget( caption )
{
}
};
boost::shared_ptr<widget> storage;
void store( boost::shared_ptr<widget> w )
{
storage = w;
}
void printCount()
{
cout << "Use count: " << storage.use_count() << endl;
}
void reset()
{
storage.reset();
}
int main()
{
lua_State *L;
L = lua_open();
luaL_openlibs( L );
luabind::open( L );
module(L)
[
class_<
widget,
widget_wrapper,
boost::shared_ptr<widget>
>("widget")
.def(constructor<string>()),
def("store", store ),
def("printCount", printCount ),
def("reset", reset )
];
if( luaL_dofile( L, "test.lua" ) )
{
object error( from_stack( L, 1 ) );
cout << error << endl;
}
lua_close(L);
cout << "lua closed" << endl;
printCount();
return 0;
}
test.lua
(text/x-lua, 310 B)
print 'Hello from Lua!'
class 'mywidget' (widget)
function mywidget:__init( name )
widget.__init(self, name)
end
w = mywidget('Frank')
store(w)
printCount()
w = nil
collectgarbage()
collectgarbage()
printCount()
--reset() -- Calling this b4 the end will stop the segfault happening
print 'Ending'