Giter Site home page Giter Site logo

How to handle deconstructors? about nbind HOT 5 CLOSED

charto avatar charto commented on May 14, 2024
How to handle deconstructors?

from nbind.

Comments (5)

jjrv avatar jjrv commented on May 14, 2024 1

The greeter object probably gets destroyed at some point afterwards. Node.js makes no guarantees, but in practice it seems destructors get called at least when the process exits. If you run the code with node --expose-gc, calling gc() from JavaScript should immediately call destructors of unreachable objects.

It all depends on whether Node.js decides it needs the memory used by the C++ object.

There's nothing you need to (or can) do about this in C++ code.

from nbind.

jjrv avatar jjrv commented on May 14, 2024 1

Another thing: Node.js doesn't know the size of your C++ object, and doesn't care about remaining space in the C++ heap. So if your C++ object is holding onto a lot of memory, you might want to call its .free() method on the JavaScript side, which will then call the C++ destructor. That keeps only a tiny JavaScript wrapper object around, which Node.js manages and knows the size of.

In practice this shouldn't be a problem.

from nbind.

jjrv avatar jjrv commented on May 14, 2024

Are you targeting a native Node.js addon or Emscripten? In Node.js addons the destructor gets called automatically from the JavaScript garbage collector. In Emscripten you can call the object's free() method from JavaScript or enable the light garbage collector to do so automatically on the next tick.

You don't need to add anything in the NBIND_CLASS block because when nbind deletes the object, C++ semantics handle calling its destructor automatically.

from nbind.

subwaystation avatar subwaystation commented on May 14, 2024

I am targeting a native Node.js addon.

Just to clarify for me:
JavaScript code:

var greeter = new Greeter();
var greeter = undefined;

This destroys the greeter object 'automatically' by the JavaScript garbage collector (as soon as ';' after undefined is passed or later)? I do not have to care about the destructor in the C++ code and I do not care about binding it with nbind?

from nbind.

subwaystation avatar subwaystation commented on May 14, 2024

Thanks for the detailed explanations. 👍

from nbind.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.