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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:22:58+00:00 2026-05-27T05:22:58+00:00

I have a CADisplayLink that triggers a draw method in a Director object. I

  • 0

I have a CADisplayLink that triggers a draw method in a Director object. I want to invalidate the CADisplayLink and then deallocate some singleton Cache objects that are used by the Director object. The singleton Cache objects are not retained by the draw method.

In a method called stopAnimation in the Director (this method is unrelated to the draw method), I do:

[displayLink invalidate];

and then I start releasing the singleton Cache objects, but then the CADisplayLink fires and the draw method gets called one last time. The draw methods tries to access the deallocated singleton objects and everything crashes.

This only happens sometimes: there are times in which the app doesn’t crash because the Cache objects are released after the displayLink is actually invalidated and the draw method has already finished running.

How can I check, after invalidating the displayLink, that the draw method has finished running and that it won’t fire again, in order so safely invalidate the Cache objects? I don’t want to modify the draw method if possible.

I tried a number of combinations, including performing displayLink invalidate on the main thread using

[self performSelectorOnMainThread:@selector(stopAnimation) withObject:self waitUntilDone:YES]

or trying to perform it in the currentRunLoop by using

[[NSRunLoop currentRunLoop] performSelector:@selector(stopAnimation) target:self argument:nil order:10 modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];

but the results is always the same, sometimes it releases the shared Caches too early.

I also don’t want to use the performSelector:withObject:afterDelay: method with an arbitrary delay. I want to make sure the displayLink is invalidated, that the draw method ended, and that it won’t be run again.

  • 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-27T05:22:59+00:00Added an answer on May 27, 2026 at 5:22 am

    This might be a bit late but since there has been no answers…

    I do not think, your selector is called once more, but rather the display link’s thread is in the middle of your draw frame method. In any case, the problem is quite the same.. This is multithreading and by trying to dealloc some objects in one thread while using them in another will usually result in a conflict.

    Probably the easiest solution would be putting a flag and an “if statement” in your draw frame method as

    if(schaduledForDestruction) {
    [self destroy];
     return;
    }
    

    and then wherever you are invalidating your display link set “schaduledForDestruction” to YES.

    If you really think the display link calls tis method again, you could use another if inside that one “destructionInProgress”.

    If you do not want to change the draw frame method, you could try forcing a new selector to the display link…

    CADisplayLink *myDisplayLink;
    BOOL resourcesLoaded;
    SEL drawSelector;
    
    - (void)destroy {    
        if(resourcesLoaded) {
            [myDisplayLink invalidate];
            //free resources
            resourcesLoaded = NO;
        }    
    }
    - (void)metaLevelDraw {
        [self performSelector:drawSelector];
    }
    - (void)drawFrame {
        //draw stuff
    }
    - (void)beginAnimationing {
        drawSelector = @selector(drawFrame);
        myDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(metaLevelDraw)];
        [myDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }
    - (void)endAnimationing {
        drawSelector = @selector(destroy);
    }
    

    or just consider something like this (but I can’t say this is safe. If the newly created display link can run the selector on a different thread then the original, it solves nothing)..

    CADisplayLink *myDisplayLink;
    BOOL resourcesLoaded;
    
    - (void)destroy {    
        if(resourcesLoaded) {
            [myDisplayLink invalidate];
            //free resources
            resourcesLoaded = NO;
        }    
    }
    - (void)drawFrame {
        //draw stuff
    }
    - (void)beginAnimationing {
        myDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
        [myDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }
    - (void)endAnimationing {
        [myDisplayLink invalidate];
        myDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(destroy)];
        [myDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a photography site that I want to prevent image copying from. How can
Have some dates in my local Oracle 11g database that are in this format:
Have tried all the embedding approaches listed on W3 schools and then some more.
Have a bunch of WCF REST services hosted on Azure that access a SQL
I have a simple game that renders 2D graphics to a frame buffer (not
In my iPhone application, I have a secondary thread that I have made for
I would like to know if it is possible to have two CADisplayLink in
Does UIView's -drawRect: method have to be drawn on the main thread or can
Have an app that can use tts to read text messages. It can also
I'm starting to work with OpenGL in iOS. I have always learned to draw

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.