Garbage Collector freeing objects that are still in use
Bryce McKinlay
bryce@mckinlay.net.nz
Wed Sep 3 14:34:00 GMT 2008
On Wed, Sep 3, 2008 at 2:37 PM, Matthijs van de Water
<matthijs.van.de.water@gmail.com> wrote:
> Here, new_item gets freed (after some time, at a completely different
> point in the code), even though I still have it inside the table and
> still need it.
>> I have a feeling this may have to do with Java variables and C++
> variables and the GC not being able to detect that a non-Java variable
> has a reference to a Java object (or something like that).
That is correct. You can't store a pointer to a Java object in a
non-Java object. If the non-Java object was allocated outside of the
GC's memory, then there is no way for it to see Java pointers stored
inside. GCJ's GC will scan the stack for pointers, but not malloc'ed
memory.
Some possible solutions:
- Store your pointers to Java objects in a HashMap or other Java
container type rather than a C++ container. This should be easy enough
if your key is an integer or String.
- Allocate memory for your C++ objects using _Jv_AllocRawObj. The GC
will scan this memory for Java pointers.
- Store Java pointers only on the stack.
Bryce
More information about the Java
mailing list