Garbage collection

Previous: Values and objects

As you probably know, Javascript and its implementations rely on garbage collection. You can call the garbage collector explicity, like this:

 flusspferd::gc();

But Flusspferd or, rather, the underlying Javascript engine, will call the garbage collector itself from time to time. Therefore, you need to protect your objects and non-immediate values (undefined, null, boolean values and small integer values are immediate) from garbage collection. You can do so either by making sure they are reachable, for example by making them a member of another "rooted" object, for example the global object, or by explicitly protecting them by making them root values. There is also a third more advanced possibility that we won't cover now (see flusspferd::tracer and flusspferd::native_object_base::trace).

The simplest way to create rooted values is by creating a flusspferd::local_root_scope. While it is in scope, all newly created objects and non-immediate values will be root values. They will cease to be root values once the local_root_scope is no longer in scope.

flusspferd::object o1 = flusspferd::create<flusspferd::object>();

{
    flusspferd::local_root_scope scope;

    flusspferd::object o2 = flusspferd::create<flusspferd::object>();

    flusspferd::gc();

    // o1 is probably deleted now, o2 is still alive.
}

flusspferd::gc();

// Now, o2 is probably deleted, too.

Alternatively, you can create root values directly:

 flusspferd::root_value v;
 flusspferd::root_string s;
 ...

Next: Binding C++ functions


Contact us at team -AT- flusspferd -DOT- org or as described on our homepage.

Generated on Thu Feb 4 23:05:12 2010 for Flusspferd by doxygen 1.6.1