user
user

Reputation: 5390

SWIG-wrapped objects in Tcl; destructors not being called on exit

I have the following scenario:

SWIG-wrapped class A (contains a std::thread which is joined on destruction, and which is running its own Tcl interpreter) Tcl interpreter prompt %

% A a
% a -delete
% exit

The above works just fine. GDB reports that the thread spawned by A a, owned by object a, is stopped.

% A a
% exit

The above results in the destructor of the underlying C++ class not being called, the thread continuing to run, and havoc to break loose on program exit (in the still running thread, which is quite unprepared for its sudden loss of its existence). This is fine and dandy, because it's program exit, but it doesn't leave me any less quizzical about why the destructor isn't being called.

Is there some sort of setting in SWIG that I can use to tell it to clean up after itself?

(deliberately not tagged with C++, because this question really isn't about C++...)

Upvotes: 0

Views: 238

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137637

Tcl does not delete memory structures when it responds to an exit because that is usually a total waste of time. Finalization is expensive, and the OS is typically far better at it than applications.

But there are exceptions. You've got one of them. What you need to do is install an exit handler with Tcl_CreateExitHandler that closes down the thread despite the object still existing. I don't know how to integrate this with SWIG.

The other approach is to put your code in a sub-interpreter (see the interp command) and make exit just quit that sub-interpreter. Then the commands will be deleted neatly (and the master interpreter can just cleanly go away once it sees the child finish).

Upvotes: 1

Related Questions