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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:08:02+00:00 2026-05-28T04:08:02+00:00

I am using CATiledLayer as backing layer for my UIView, which I have put

  • 0

I am using CATiledLayer as backing layer for my UIView, which I have put inside UIScrollView. In init method of my view I am creating CGPathRef object which draws simple line. When I am trying to draw this path inside drawLayer:inContext it occasionally crashes with EXEC_BAD_ACCESS (rarely) when I am scrolling / zooming.

The code is very simple, I am using only standard CG* functions:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
        tiledLayer.levelsOfDetail = 10;
        tiledLayer.levelsOfDetailBias = 5;
        tiledLayer.tileSize = CGSizeMake(512.0, 512.0);

        CGMutablePathRef mutablePath = CGPathCreateMutable();
        CGPathMoveToPoint(mutablePath, nil, 0, 0);
        CGPathAddLineToPoint(mutablePath, nil, 700, 700);
        path = CGPathCreateCopy(mutablePath);
        CGPathRelease(mutablePath);
    }
    return self;
}

+ (Class) layerClass {
    return [CATiledLayer class];
}

- (void) drawRect:(CGRect)rect {
}

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
    CGContextFillRect(ctx, self.bounds);

    CGContextSetLineWidth(ctx, 5);

    CGContextAddPath(ctx, path);
    CGContextDrawPath(ctx, kCGPathStroke);
}

- (void)dealloc {
    [super dealloc];
}

UPDATE:
I have noiced that this problem exists only on iOS 5, it works fine on 4.3

  • 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-28T04:08:02+00:00Added an answer on May 28, 2026 at 4:08 am

    I ran into a similar issue when attempting to draw cached CGPath objects on a custom MKOverlayView.

    The crash may occur because a CGPath can’t be simultaneously drawn on multiple threads – it’s an opaque class which (as specified in the documentation) contains a pointer to the current point in its points array. Two or more threads iterating over this array simultaneously while they draw it could lead to undefined behavior and a crash.

    I worked around this by copying the CGPath object into each drawing thread (contained within a mutex lock to prevent incomplete copying):

    //lock the object's cached data
    pthread_mutex_lock(&cachedPathMutex);
    //get a handle on the previously-generated CGPath (myObject exists on the main thread)
    CGPathRef myPath = CGPathCreateCopy(myObject.cachedPath);
    //unlock the mutex once the copy finishes
    pthread_mutex_unlock(&cachedPathMutex);
    
    // all drawing code here
    CGContextAddPath(context, myPath);
    ...
    ...
    CGPathRelease(myPath);
    

    If you’re concerned about the memory overhead of doing a copy on each thread, you can also work directly on the cached CGPath objects, but the mutex will have to remain locked during the whole drawing process (which kind of defeats the purpose of threaded drawing):

    //lock the object's cached data
    pthread_mutex_lock(&cachedPathMutex);
    
    //get a handle on the previously-generated CGPath (myObject exists on the main thread)
    CGPathRef myPath = myObject.cachedPath;
    
    // draw the path in the current context
    CGContextAddPath(context, myPath);
    ...
    ...
    
    //and unlock the mutex
    pthread_mutex_unlock(&cachedPathMutex);
    

    I’ll qualify my answer by saying that I’m not an expert on multithreaded drawing with Quartz, only that this approach solved the crashes in my scenario. Good luck!

    UPDATE:
    I revisited this code now that iOS 5.1.0 is out and it looks like the root cause of the issue may have actually been a bug in Quartz in iOS 5.0.x. When testing on iOS 5.1.0 with the CGPathCreateCopy() and mutex calls removed, I’m seeing none of the crashes experienced on iOS 5.0.x.

    //get a handle on the previously-generated CGPath (myObject exists on the main thread)
    CGPathRef myPath = myObject.cachedPath;
    
    // all drawing code here
    CGContextAddPath(context, myPath);
    ...
    ...
    //drawing finished
    

    Since chances are we’ll be supporting iOS 5.0.x for a while, it won’t hurt to keep the mutex in your code (other than a slight performance hit), or simply run a version check before drawing.

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

Sidebar

Related Questions

I have a large UIScrollView which contains a CATiledLayer which I'm using to draw
I have a CATiledLayer inside a UIScrollView and all is working fine. Now I
I have a Graph being drawn inside a UIScrollView . It's one large UIView
I have a UIView subclass that I'm drawing a PDF onto (using a CATiledLayer).
I have a UIView inside a UIScrollView . Whenever the UIScrollView zoom changes, I
I have a UIScrollView with a custom content view that uses drawRect to display
Using MVC2 I have an AJAX form which is posting to a bound model.
I have a strange issue where I'm scrolling through a paged UIScrollView which displays
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have

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.