CAContextInvalidLayer and setFence:count

So there I was, up for nearly a day straight and have figured out the certificate issue that was preventing my program from running on my iPhone after upgrading to SDK 3.0 beta 5.

On the device I was getting this error: CAContextInvalidLayer – CALayer already attached to a context while on the simulator I was getting this error: setFence:count: called more than once per transaction. Cryptic messages that googling for solutions found no answers to.

After many hours of investigation and trial and error, I checked out older revisions of my code base (gotta love svn, or any revision control system for that matter!) until I arrived at a build that installed and ran on the device. From there I started doing file by file compares with the next version (which didn’t install) looking for anything that might be the cause.

Finding nothing obvious, I started implementing each change one by one. The difference between the revisions were minor and few in count (as any good checkin should be) so this didn’t take long… and I arrived at a solution!

My application was using several layers to create visual effects. At one point I made a “change that will make no difference”, and so minor I kept ignoring it in my code reviews! However, this code was the cause of all of my issues… but only long after the actual code had been run.

The problem came down to how I created my layers. I had started out very basic, but then thought it would be better to make a copy of the first layer so I could inherit more of it’s properties. However, this code:

targetLayer = [[CALayer layer] initWithLayer:self.layer];

copied too much information and when I later tried to work with the layers I ran into the “already attached” problem. The correct way to create layers for my program (and what I started out with) is:
targetLayer = [CALayer layer];

Reverting that one change in my latest code base let me get back to making progress. Hopefully documenting my error here will save someone else a few minutes or hours if they run into the same issue.

About Lane Roathe

I make my living designing, producing, and programming computer software. I have done work on numerous platforms, including MacOS, Win95/NT, Apple //, Super Nintendo, Sega, and others; writing mainly games but also operating systems, applications, utilities, etc. I have started and run several companies, currently operating Ideas From the Deep.
This entry was posted in iOS and tagged , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s