«

»

Mar
16

Interesting bug in Cocoa

So in the moment I’m working on something that requires relatively smooth animations, so I figured the best way to do that is to do that with either – a timer, or super-slow drawing steps (meaning instead of moving at least 1 px in a certain amount of time, use no timer but increase pixel location by fractions).

Now in Cocoa (Objective-C to be precise) there are two ways to accomplish this. The one that follows appears to be flawed. So if you do

[[NSRunLoop currentRunLoop]
runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]];

(where 0.1 is 0.1 seconds), it appears to spike memory usage and increase garbage. My application used 23 MB at opening – but when animating using this code, I suddenly used up a whopping 800 MB after 5 seconds of animation.

So my advise is to use this method, which basically has the same effect. However you also need to define a method, define if the timer repeats or not, and the time frame.

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
target: self
selector: @selector(animate)
userInfo: nil
repeats: YES];

[timer fire];

No idea what userInfo is needed for… just leave it at nil. This method does not increase memory usage at all.




Related posts:

  1. Living on the EDGE
  2. Interesting offer in Dublin City

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>