Glenn Chambers wrote:
> On Sat, 2008-12-13 at 17:55 -0500, Daniel Walter wrote:
>> As I look through the documentation of CNI, I have not found anything about
>> how Garbage Collection is done. Is there a section on this?
>
> It's been a few years since I actively tracked the project, so I can't
> help with the documentation. I do, however remember the answers to your
> questions.
>
>> 1. In section 11.8 of the manual, a hashtable is created with new
>> java::util::Hashtable(120). How is this deallocated? Based on what I have
>> seen, it would need to be deleted in C++ which would release it to the GC to
>> decide when Java is done with it for actual destruction. Is this correct?
>
> Java class allocation is automatically done via the Boehm garbage
> collector. Invoking the 'delete' operator is not required, and will
> fail. (I don't remember if it is a compile error or not.)
>
>> 2. If I get a reference to an object as a result of a function call, how
>> does this effect Garbage Collection of this object. Do I need to delete
>> this object for Garabage Collection.
>
> If the object is a Java object, then there will be a new reference to it
> for the lifetime of the function. If the original reference is erased
> in the course of program execution, the reference in the function will
> prevent the object from being collected until the function exits.
I think a bit more clarification is required.
If you get a reference to a Java object as a result of a function call, you
must store that reference somewhere that is reachable by the collector or
that object will be collected. The collector scans all fields of all objects,
so something like
MyClass::foo = objectRef;
is sufficient. Storing the reference in an object in the C++ heap will *not*
do.
Andrew.