Memoryhandling with ReleaseObject and CreateRotatedImageMap()

Hi all,

I stumbled over a behaviour that I cant completely understand.

Given the documentation for the CreateImageMap():

If this function is successful the ImageIn is shared by the ImageOut to ensure its life-time. When not needed anymore the ImageOut must be released via ReleaseObject. In that case also the ImageIn is released.

And the following code:

IMG image;
cvbbool_t success = LoadImageFile("PathToSomeBigImage", image); 
	
IMG imagerotated;
CreateRotatedImageMap(image, VPATROT_90, imagerotated);
 
// Keeps storage 
ReleaseObject(imagerotated);
ReleaseObject(image);
// until here

I would have assumed, that the first ReleaseObject() call would have also freed the storage that was allocated by image.
However this storage is allocated until the second ReleaseObject() call on the original image was successful.

Do I miss something?

Cheers
Chris

Nope…

ReleaseObject's primary effect is that it decreases the reference count. No more, no less than that. If the reference count happens to reach zero, the object is wiped from memory - but that is more of a side effect of ReleaseObject than anything else.

And the documentation of CreateRotatedImageMap() states the following:
image
“released” does not say or imply that ReleaseObject on ImageOut does free the memory! This only happens (see above) if the circumstances are right…

For your code snippet one can actually easily write down what happens in terms of reference counts:

# Step Reference count image afterwards Reference count imagerotated afterwards Comment
1 LoadImageFile(..., image) 1 undefined
2 CreateRotatedImageMap(image, ..., imagerotated) 2 1 imagerotated uses memory held by image, so imagerotated must make sure that image lives at least as long as imagerotated; it does so by holding a reference count on image open…
3A ReleaseObject(imagerotated) 1 undefined imagerotated has now been deleted from memory and the reference count held open on image has been released; image still exists!
4A ReleaseObject(image) undefined undefined both images are gone now

Note that it would be possible (and perfectly legitimate) to swap step 3 and 4:

# Step Reference count image afterwards Reference count imagerotated afterwards Comment
1 LoadImageFile(..., image) 1 undefined
2 CreateRotatedImageMap(image, ..., imagerotated) 2 1 imagerotated uses memory held by image, so imagerotated must make sure that image lives at least as long as imagerotated; it does so by holding a reference count on image open…
3B ReleaseObject(image) 1 1 both images continue to exist! however, it would not be advisable to use the handle image from now on because the ReleaseObject actually means “I won’t need this any more”
4B ReleaseObject(imagerotated) undefined undefined both images are gone now
2 Likes

Hi @illusive

thank you for that detailed answer.
It highlithed what I was thinking what would happen as well.

Was the part that made me suspicious.
What I understood was, that if you release ImageOut, then also ImageIn will be released.

Cheers
Chris

Yes, I think that was the issue. The point is to figure out what “release” actually means. It does mean that the reference count is decreased, but it does not necessarily mean that the memory gets freed.