Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6368543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:41:21+00:00 2026-05-25T00:41:21+00:00

I am caching some graphics onto CGLayers and then storing them in NSValue objects

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:41:21+00:00Added an answer on May 25, 2026 at 12:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm planning on caching some information using ehcache. Normally, I would follow the pattern:
What would be some advantages of using etags/stale?/fresh_when? instead of page caching (on a
I'm trying to do some caching using VaryByParam AND VaryByHeader. When an AJAX request
I am caching some data using ASIDownloadCache as shown below. ASIHTTPRequest *request = [ASIHTTPRequest
I'm implementing some caching by using the nifty Rails.cache.fetch . However, in one particular
I'm trying to implement some simple caching into my application and I'm having some
There is a caching policy for some organizations. for example, if you want to
I would like to create a caching system that will bypass some mechanisms in
I have been researching and have some few ideas about a distributed caching system
Due to some caching issues, I need to explicitly bypass the cache, for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.