Skip to content

Commit

Permalink
Java bindings: avoid double free when calling Dataset.Close()
Browse files Browse the repository at this point in the history
Fixes #11566
  • Loading branch information
rouault committed Jan 2, 2025
1 parent 46372fe commit 64143e0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions swig/include/Dataset.i
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public:
}
}

#ifdef SWIGJAVA
%rename (CloseInternal) Close;
%javamethodmodifiers Close() "private";
#endif
CPLErr Close() {
return GDALClose(self);
}
Expand Down
11 changes: 11 additions & 0 deletions swig/include/java/gdal_java.i
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ import org.gdal.gdalconst.gdalconstConstants;
}
%}

%extend GDALDatasetShadow {
%proxycode %{
public int Close() {
int ret = gdalJNI.Dataset_CloseInternal(swigCPtr, this);
swigCPtr = 0;
swigCMemOwn = false;
return ret;
}
%}
}

%typemap(javacode) GDALDatasetShadow %{

// Preferred name to match C++ API
Expand Down
3 changes: 3 additions & 0 deletions swig/java/apps/GDALTestIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ private static void testInt64() {
if (data1[i] != data2[i])
throw new RuntimeException("int64 write and read values are not the same "+data1[i]+" "+data2[i]);
}

dataset.Close();
dataset.Close();
}

private static void testGetMemFileBuffer()
Expand Down

0 comments on commit 64143e0

Please sign in to comment.