0

I am caching some graphics onto CGLayers and then storing them in NSValue objects using @encode (so as to store them in an array). I just wanted to make sure that I handle the retain/release correctly...

I cache the graphics and store them in the array something like this:

// Create an NSMutableArray "newCache"
CGLayerRef drawingLayer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef drawingContext = CGLayerGetContext(drawingLayer);

// Do some drawing...

[newCache addObject:[NSValue valueWithBytes:&drawingLayer objCType:@encode(CGLayerRef)]];
CGLayerRelease(drawingLayer);      // Is this release correct?

And then later on I retrieve the layer:

CGLayerRef retrievedLayer;
[(NSValue *)[cacheArray objectAtIndex:index] getValue:&retrievedLayer];

// Use the layer...

// Should I release retrievedLayer here?

Am I right to assume that the layer needs releasing after being added to the array (the last line in the first code snippet)? I assumed this is the case since I called a create function earlier in the code. Is the NSValue then keeping track of the layer data for me? Does the retrievedLayer need manually releasing after being used?

Thanks

Stuart
  • 36,683
  • 19
  • 101
  • 139
  • Is there a reason you're using an array? You may find NSCache both easier to use and more helpful when memory runs low. – Peter Hosey Aug 28 '11 at 16:26
  • A fair point - no, I'm only using an array because there are enough layers to require a collection. I've not actually used NSCache before, but this looks like it might be an ideal candidate for it. – Stuart Aug 28 '11 at 16:53

1 Answers1

1

NSValue doesn't know about Core Foundation types, so it will not retain or release the CGLayer. (The @encoded type string pretty much just tells it how big the value is; it does not tell it anything about memory management.)

You must not release the layer until you are fully done with both the layer and the NSValue.

Or, better yet, just put the CGLayer into the array. All Core Foundation objects are compatible with NSObjects for purposes of memory management (discussed previously), which has the practical effect that you can put CF objects into NSArrays and vice versa. Since CGLayers are CF objects, this means that you can put the CGLayer into the array without boxing it in another object.

Community
  • 1
  • 1
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Thanks for the reply. I've solved the problem now - since each layer could be retained by multiple separate classes I found it difficult to handle the releases properly. I was failing to account for the "original" layer in the class implementing the cache, after realising that I couldn't release it immediately after storing it in the array. I ended up iterating through the cache array in dealloc to release any hangers-on. Thanks again. – Stuart Aug 28 '11 at 16:51
  • @StuDev: Arrays retain their contents, so if you put the CGLayers into the array directly, the array will retain them. This goes for NSCache as well. – Peter Hosey Aug 28 '11 at 17:03
  • Am I right in thinking that since the CGLayers were inserted into the array via `valueWithBytes:objCType:` the CGLayers aren't actually retained by the array? Instead, I simply didn't release the CGLayers at the time of inserting them into the array in order to keep them around. I had to iterate through the array in dealloc to find the references to the layers and call the CFRelease() methods, since the array doesn't appear to do this for CF types. – Stuart Aug 28 '11 at 17:13
  • @StuDev: Correct. You didn't insert the CGLayers (directly) into the array; you inserted NSValue objects. The array retained the NSValue objects and, as such, releases the NSValues at dealloc. It does not know or care about any objects owned by the NSValue objects, because it should not—that would be a violation of the memory management rules. The objects in the array—the NSValues—are responsible for releasing any objects they own, and they do not own (retain) the CGLayer objects. This is why it's better to put the CGLayers directly into the array: Then the array will retain and release them. – Peter Hosey Aug 28 '11 at 21:34
  • How would I put the CGLayers directly into the array? Do I need to use a CFArray? – Stuart Aug 29 '11 at 14:48
  • @StuDev: Same way you put any other object into an NSArray. No. – Peter Hosey Aug 29 '11 at 16:51
  • Oh really? I get the warning: "Passing argument 1 of 'addObject:' from incompatible pointer type". Casting as `id` removes the warning - is this an acceptable thing to do? – Stuart Aug 29 '11 at 17:05
  • @StuDev: It is a CF object, which is good enough for this purpose—as I said in the answer, you can put CF objects into NSArrays (or NSCaches). And yes, that is the way to suppress the warning. – Peter Hosey Aug 29 '11 at 17:33
  • @StuDev, I am also storing CgLayers into NSMuatbleDictionary, for implementing my undo and redo functionaliy, but I am getting stuck in retrieving it, and whether Its the right approach for undo redo? please have a look at this link http://stackoverflow.com/questions/11394839/undo-redo-issues-with-cglayer – Ranjit Jul 10 '12 at 11:47
  • Hello @PeterHosey, please have look at this question http://stackoverflow.com/questions/21027788/drawing-multiple-images-with-different-size-on-cglayer – Ranjit Jan 10 '14 at 07:16