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 4000480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:47:13+00:00 2026-05-20T07:47:13+00:00

I ran into the strangest of problems: sometimes adding a sublayer to a CALayer

  • 0

I ran into the strangest of problems: sometimes adding a sublayer to a CALayer does not work ([layer addSublayer:child]), i.e. the sublayer count remains at 0 and nothing is drawn to the screen.

Sometimes it does work and the child layer shows up on the screen as I would have expected. When it does not, usually performing a clean build fixes it, but not always. I also experimented with running two instances of my app from the Terminal, usually the second instance shows the problematic behavior.

Here is the (abbreviated) code:

   NSLog(@"Retain count before: %d", [childLayer retainCount]);
   // [parentLayer insertSublayer:childLayer atIndex:0]; // same problem here
   [parentLayer childLayer];
   NSLog(@"Retain count after: %d layer: %@", [childLayer retainCount], childLayer);
   NSLog(@"Sublayercount: %d", [[parentLayer sublayers] count]);

The child layer is a basic layer with the background set to a RGB color.

Debug output:

layer: <CALayer: 0x1982b0> X: 50.00 Y: -90.00 width: 200.00 height: 200.00
Retain count before: 2
Retain count after: 2 layer: <CALayer: 0x1982b0>
Sublayercount: 0

When it does work, the above output shows the retain count being increased correctly to 3 and the sublayercount to 1.

Is there something special about CALayers? Does addSublayer rely on something else/perform validity checks of the sublayer?

Can anybody suggest a way to debug this? Is there a way how I can step into addSublayer with the debugger?

Thanks! Mark.

  • 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-20T07:47:13+00:00Added an answer on May 20, 2026 at 7:47 am

    I finally figured it out. Sometimes it helps to just talk (well, post) about the issue.

    It turned out to be a problem with the way I initialized the custom view that holds the layers. There was a race condition between the awakeFromNib calls to the app controller and the view. The app controller would load the custom layers, but sometimes the custom view was not yet initialized.

    The solution was to move the initialization code for the view in the app controller from awakeFromNib to appliationDidFinishLaunching.

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

Sidebar

Related Questions

I ran into the problem that my primary key sequence is not in sync
I ran into a situation today where Java was not invoking the method I
(this is regarding the Ramaze.net framework) I ran into some really strange problems while
I've just started with opengl but I ran into some strange behaviour. Below I
I ran into a scenario where LINQ to SQL acts very strangely. I would
I ran into an issue with an IIS web app shutting down an idle
I ran into an interesting (and very frustrating) issue with the equals() method today
I ran into an interesting behavior recently. It seems that if I override .equals()
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
I ran into an issue with something that I am probably just overlooking. I

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.