views:

701

answers:

5

On iPhone.. Why would code such as this cause memory leak? after 2 minutes the net bytes have doubled. All I'm doing is moving a ball round the screen with an NSTimer calling the below method.

Any ideas?

- (void)nextFrame:(NSNotification *)notification {
    ballInstance.frame = CGRectMake(value, 0,  320, 480);
}
+8  A: 

The posted code has no leak. The problem is elsewhere.

If you know that there's a leak inside of nextFrame:, it has to be in -[Ball setFrame:] because it is the only message sent in this method.

Nikolai Ruhe
But if I remove the line "ballInstance.frame = CGRectMake(value, 0, 320, 480);"...no leak occurs. And as that line has no further interaction with the rest of my code I presumed the line to be the troublemaker.
@unknown: As Nikolai said, the leak must be somewhere in the setFrame: method that you're calling, because it certainly isn't in CGRectMake().
Chuck
unknown: Remember, property assignments (`ballInstance.frame = …`) are messages. A property assignment and a setter message expression (`[ballInstance setFrame:…]`) are exactly the same thing. You are calling the `setFrame:` method, and if cutting out that message stops the leak, then that method is responsible for the leak.
Peter Hosey
+3  A: 

The leak is not in the code you show, especially if frame is a @synthesized property. You either need to show more code, or spend some quality time with Instruments to figure out what is being leaked and where it is being allocated.

Barry Wark
A: 

here is the 'full' code, new project, still behaves the same. It moves a jpg accross the screen, and as it does memory is massively consumed. If I remove the '++' from 'value' memory is fine. (in otherwords have a static graphic) So.... is the image being cached is the question? If so how can i stop it reaching astronomical sizes?

- (void)applicationDidFinishLaunching:(UIApplication *)application {    


    [window makeKeyAndVisible];

    NSTimer * nSTimer =[NSTimer scheduledTimerWithTimeInterval: .02
         target: self
       selector: @selector(tick)
       userInfo: nil
        repeats: YES];
    value =0;
}

- (void)tick {
    NSLog(@"tick");
    myOutlet1.frame = CGRectMake(value++, 0,  320, 480);
}
an image of instruments is here.. http://privateevent.co.uk/underground/img/gallery/19_1254856341.png
This is not the full code. The definition of `value` for example is missing. But are you sure that there's a leak? What information exactly makes you think that there's a leak. Be more specific or post the whole project.
Nikolai Ruhe
This is not an answer. If you want to add more information to the question, edit the question, don't post it as an answer when it isn't.
Chuck
value is a float initialized to 0; There is no other relevant code.myOutlet1 connects to a UIImageView in a xib.Memory consumption rises and rises as in this image here... http://privateevent.co.uk/underground/img/gallery/19_1254856341.png
Chuck - ah ok, i did wonder if that was correct or not.
+2  A: 

According to Apple "This is a bug in iPhone OS 3.0. The allocator for the graphics system
is reporting realloc events as malloc events, so ObjectAlloc tallies
these as new objects that are almost never being freed. I'm not
certain why you might not see it when you add the Leaks tool, but
neither tool would show a true leak for this."

from here.. http://lists.apple.com/archives/xcode-users/2009/Sep/msg00278.html

Though I'm still none the wiser as to how to remedy it.

So, basically just ignore it and your fine! Very annoying, wasted most of a day.
+1  A: 

I've posted a complete sample application that seems to more or less match your "new project" example above. Can you take a look at it and see if this gives you any ideas? I've run it on the simulator and on the device w/ no leak.

http://static.fatmixx.com/MemTestApp.zip

It really does look like there is NO leak here. I'm building against iPhoneOS 3.1 - Debug.

Sujal

sujal