Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS SWIG: C++ destructor not being called on exit #329

Open
Zion-ICS opened this issue Feb 6, 2015 · 3 comments
Open

JS SWIG: C++ destructor not being called on exit #329

Zion-ICS opened this issue Feb 6, 2015 · 3 comments

Comments

@Zion-ICS
Copy link

Zion-ICS commented Feb 6, 2015

Steps to reproduce:
compile and install SWIG
run make check-examples
go to Examples/javascript/class
open runme.js and replace the top line with
var example = require("./build/Release/example");
Run the example

Note that it says that 2 shapes remain even after all the objects were destroyed.

We're running into the same issue using SWIG.
We need the destructor to be called because our destructors are cleaning up important state behind-the-scenes. It'd be a shame to have to provide interfaces to clean up everything just because the SWIG destructor isn't working.

If there were at least an interface to call the destructor, this bug wouldn't be as bad. But there's no delete_class function (note that python has new_class and delete_class functions).

@Zion-ICS Zion-ICS changed the title JS destructor not being called for classes JS SWIG: c++ destructor not being called on exit Feb 9, 2015
@Zion-ICS Zion-ICS changed the title JS SWIG: c++ destructor not being called on exit JS SWIG: C++ destructor not being called on exit Feb 9, 2015
@Zion-ICS
Copy link
Author

Zion-ICS commented Feb 9, 2015

I'm not sure whether the destructor for JavaScript was ever run on exit.
I just checked-out version 3.0.1, the first release with JavaScript examples.
I got the same result-- the destructor isn't being called.

I'm running Ubuntu 14.04 on a 64-bit machine.

@ojwb
Copy link
Member

ojwb commented Feb 8, 2022

I don't think you can in general rely on the C++ destructor getting called on process exit for bindings in garbage collected target languages - that's not something SWIG can really control as it is down to the target language whether the proxy object gets destroyed (and finalised if that's a separate stage) in this situation, which is what's needed to call the C++ destructor. It seems with node it doesn't.

Looking at Examples/javascript/class/runme.js that already explicitly does delete c; and delete s; yet reports (without the need for the change suggested above even):

2 shapes remain

So for node it seems the C++ destructor only gets triggered if/when GC runs and finalises the object after a delete on the JS side.

However adding an explicit global.gc(); after the delete calls (which requires running with node --expose-gc) only changes that to:

1 shapes remain

So for some reason it seems the GC fails to reclaim both the objects here.

@ojwb
Copy link
Member

ojwb commented Jun 21, 2022

Perhaps the way to resolve this then is to expose the existing _wrap_delete_Shape C++ function as delete_Shape in Javascript, (similar to what Python does apparently).

@ojwb ojwb added the C++ label Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants