Using Java objects from C++
Bryce McKinlay
mckinlay@redhat.com
Mon May 3 20:20:00 GMT 2004
Dave Menendez wrote:
>I'm not familiar with the release cycle of GCC. Would we have to wait
>for the next formal GCC release for this added functionality, or since
>we have the source code, could a patch be posted whenever this is
>ready? Just curious - I'm not trying to make demands of anyone's time.
>>
Most changes go into CVS HEAD in the first instance, which will
eventually become gcc 3.5. Since this should be a low-risk addition, we
can also put it on the stable 3.4 branch for inclusion in 3.4.1. You
could apply the patch yourself or just check out the latest 3.4 branch
tree from cvs.
(In the future we hope to be able to provide interim updates to libgcj,
independent of compiler releases - but this isn't really possible right
now, for various reasons.)
>Is explicit registration of static roots something that is
>already available or something you would be planning to add in? I may
>just have to dig around the header files some more if it's already
>something I should be doing with the 3.4.0 GCC release.
>>
It is something thats been on the wish-list for quite a while, but we
haven't implemented it yet. We should be a step closer, however, once
the "global ref" functionality is implemented.
>>>P.S. One more thing, I was a bit mystified when I read in the mailing
>>>list that even though I use "new" to allocate a Java object from C++,
>>>I'm not supposed to call "delete" to get rid of it, but that it somehow
>>>automagically gets collected by the GC. If someone could provide a
>>>brief description of how the GC decides what is eligible for collection
>>>or point me to a document to help me understand the logic, that would
>>>also be much appreciated.
>>>>>>>>"new" is overloaded by java::lang::Object. So when you allocate a Java
>>object, "new" actually calls the GC allocator, not the standard C++ one.
>>>>>>Maybe I need to read up on allocators in general - I understand that the
>GC allocator gets used instead, but I wasn't sure how the GC detects
>that one particular object I instantiated from C++ is no longer in use
>if I didn't add it to some other Java object. Is it automatically
>"dereferenced" somehow when the block containing the variable holding it
>ends?
>>
The key thing is that, with GCJ, there is a single stack for C++ and
Java code. So, any local variables will be seen by the collector as it
scans the stacks. If it sees a pointer to a Java object, that object
will be marked regardless of whether the stack frame was allocated by
Java or C++.
Regards
Bryce
More information about the Java
mailing list