aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2024-01-14 10:37:10 +0100
committerThomas White <taw@physics.org>2024-02-06 16:59:35 +0100
commitd0787517583955eb0d9391b1bd14764198e83903 (patch)
tree87df0a3d60434f57a7dd1f4178b5474670216af7 /libcrystfel
parent0273eb0b50ff17feb12a0a1e0ac7ee5dc1233622 (diff)
Julia: Image/PeakList memory semantics (again)
Previous commit a6462e1f0 was still not right. If the image gets freed, the PeakList can be left with a dangling reference. If the peak list gets replaced at the C level (e.g. by running a new peak search), Julia will have no way of knowing about it. Instead of having the PeakList know if it's associated with an image, it's better to have the Image know (at the C level) if it's responsible for freeing the ImageFeatureList. As soon as the ImageFeatureList is exposed to the Julia GC via a PeakList object, it becomes Julia's responsibility to free it. The Julia Image structure contains a reference to the Julia PeakList, to prevent this from happening until either the image is freed or a new PeakList is substituted. The two references (Julia image.peaklist and C image->features) have to be kept in sync, and we check image->features every time the peaklist is requested.
Diffstat (limited to 'libcrystfel')
-rw-r--r--libcrystfel/src/image.c3
-rw-r--r--libcrystfel/src/image.h4
2 files changed, 6 insertions, 1 deletions
diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c
index 3b4e04df..ab4518a9 100644
--- a/libcrystfel/src/image.c
+++ b/libcrystfel/src/image.c
@@ -1394,7 +1394,7 @@ void image_free(struct image *image)
int i, np;
if ( image == NULL ) return;
- image_feature_list_free(image->features);
+ if ( image->owns_peaklist ) image_feature_list_free(image->features);
free_all_crystals(image);
spectrum_free(image->spectrum);
cffree(image->filename);
@@ -1464,6 +1464,7 @@ struct image *image_new()
image->bw = -1.0;
image->peak_resolution = -1.0;
image->features = NULL;
+ image->owns_peaklist = 1;
return image;
}
diff --git a/libcrystfel/src/image.h b/libcrystfel/src/image.h
index beb66810..74ff6eee 100644
--- a/libcrystfel/src/image.h
+++ b/libcrystfel/src/image.h
@@ -190,6 +190,10 @@ struct image
/** Re-usable data array structure, or NULL if not used */
ImageDataArrays *ida;
+ /** If set, then 'features' should be freed with the image.
+ * Otherwise, it is managed externally (e.g. by Julia) */
+ int owns_peaklist;
+
};
#ifdef __cplusplus