[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: SWIG, C++ Exceptions, and Lua
- From: Mark Gossage <mark@...>
- Date: Thu, 20 Mar 2008 01:30:28 +0000 (GMT)
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.
Joey Mukherjee <email@example.com> 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?
Rise to the challenge for Sport Relief with Yahoo! for Good