Ok, this is helping, but after debugging further, I actually have a different, but similar problem.
I have the following code I am wrapping with SWIG:
class Plot { public: Plot (); virtual ~Plot (); ... < other Plot functions >
};
class BoxPlots { public: void AddPlot (Plot *plot); Plot *GetPlot (const unsigned int index) const; const unsigned int GetNumberOfPlots () const;};
When I construct my Plot, Lua owns this and that's fine. However, when I AddPlot to the BoxPlots, now I want to own the pointer. Is that possible with SWIG? Alternatively, is there a way with %newobject to label the constructor as me owning it all. If the user never AddPlot, I will never see the pointer so the program will leak, which is why this is worse; but it would keep my program from core dumping.
Thanks, Joey On Mar 19, 2008, at 8:30 PM, Mark Gossage wrote: Hello Joey,
1) Yes there is. On the unknown exception issue, you will need to change your %catches statement. If you changed it to %catches(std::exception&), then it can catch the report on this. I think it will also catch all the derived classes too. Normally C++ libraries have a base exception class which you can catch if you want to. On the getting a stack trace its a bit harder, as C++ exceptions don't have built in stack traces. (If anyone out there knows a good way to get stack traces into C++ exceptions, please let me know). However its easy to get the stack trace from the Lua side. Instead of using pcall(fn), use xpcall(fn,debug.traceback), this will convert the error into an error & stack trace (must update the SWIG documentation with this trick)
2) Memory management is done in the way you explained in SWIG. There is an ownership flag on every object. When a Lua userdata is gc'ed, it will check its ownership flag & its its set it will delete the attached object. By default, the flag is set on any function returning an object, but not if it returns a pointer or reference. If you have a function which returns a pointer which needs to be deleted later, use the %newobject command. eg %newobject create_A(); // warns SWIG to manage this pointer classA* create_A(); // returns a new object
Hope that helps. Regards, Mark
Joey Mukherjee <joey@swri.edu> wrote: This helped tremendously! I still have some problems which I am hoping are equally easy:
1) Once caught, is there a way to retrieve more info about what caused the problem? Something like line number and cause of exception (e.g. out of range on GetVi call at line 20)? Right now, everything comes back as "unknown exception". This might be my fault since I added this to my .i file: "%catches (...);" which catches all exceptions on my object, but that was easier than putting specific catches on all the routines of the object.
2) My C++ stuff is doing all its own memory management and sometimes I free stuff which then Lua tries to garbage collect. At least that is what I think is happening since I reuse my Lua state. Is there a way to tell Lua that I own it all? When I debug into my code, I see a check for "own" and the flag is set to false. Is there a way to force to true?
Thanks, Joey
Rise to the challenge for Sport Relief with Yahoo! for Good |