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

  • Home
  • SEARCH
  • 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 am trying to put some distributed caching into play, I'm using this indeXus.Net
I'm needing to cache some data using System.Web.Caching.Cache . Not sure if it matters,
I'm planning on caching some information using ehcache. Normally, I would follow the pattern:
I'm trying to setup some caching on my site and am having troubles with
Trying to set up caching on our datasets - we're using clr stored procedures
Problem : I have to create a number of tables for caching some amount
I have a java program that uses a weak hashmap for caching some things,
I added some caching functionality to the geokit gem and used gemcutter to create
I'm building in some caching to one of my sites, and I can't seem
I am thinking of integrating some caching logic in my app. Mainly, it will

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.