Opened 7 years ago

Closed 7 years ago

#6911 closed defect (invalid)

Are the parentReferences in the java code really needed?

Reported by: Kurt Schwehr Owned by: Even Rouault
Priority: normal Milestone:
Component: JavaBindings Version: unspecified
Severity: normal Keywords: lint gc
Cc:

Description

When are these parent references really needed? They are getting flagged in some linters as never read.

e.g. the one created in r16309

  private Object parentReference;

  /* Ensure that the GC doesn't collect any parent instance set from Java */
  protected void addReference(Object reference) {
    parentReference = reference;
  }
find . -name \*java\* -a -type f | xargs grep parentReference
./include/java/gdal_java.i:  private Object parentReference;
./include/java/gdal_java.i:    parentReference = reference;
./include/java/gdal_java.i:  private Object parentReference;
./include/java/gdal_java.i:    parentReference = reference;
./include/java/gdal_java.i:  private Object parentReference;
./include/java/gdal_java.i:    parentReference = reference;
./include/java/ogr_java.i:  private Object parentReference;
./include/java/ogr_java.i:        obj.parentReference = null;
./include/java/ogr_java.i:    parentReference = reference;
./include/java/ogr_java.i:  private Object parentReference;
./include/java/ogr_java.i:        obj.parentReference = null;
./include/java/ogr_java.i:    parentReference = reference;
./include/java/typemaps_java.i:  private Object parentReference;
./include/java/typemaps_java.i:        obj.parentReference = null;
./include/java/typemaps_java.i:    parentReference = reference;

Change History (2)

comment:1 by Even Rouault, 7 years ago

You can always try to remove them, stress-test the java garbage collector and observe crashes...

But in the snippet you quote, there's an important comment "/* Ensure that the GC doesn't collect any parent instance set from Java */"

So yes there are never read, but we need the child object to have a reference to its parents, to avoid the native parent object to be destroyed while the Java child object is used.

For ex :

ds = gdal.Open('bla.tif');
band = ds.GetRasterBand();
ds = null;
do garbage collection
band.GetXXXX()

ds could be finalized at that point and the C++ dataset closed, but since band maintains a reference to ds, this won't happen.

This is actually the issue we have with the Python bindings.

Last edited 7 years ago by Even Rouault (previous) (diff)

comment:2 by Even Rouault, 7 years ago

Resolution: invalid
Status: newclosed

I think the above comment answers the question. Closing

Note: See TracTickets for help on using tickets.