tags:

views:

98

answers:

4

very simple code, but leaks as hell... check it out.

- (void)loadView {

    NSString * myTitle = @"HELLO";
    NSString * message = @"\nThis stuff is leaking!\n";
    NSString * cancelButton = @"Dismiss";
    NSString * otherTitles = nil;
    [self showAlert: myTitle : message: cancelButton : otherTitles];


}


- (void) showAlert: (NSString *) titulo : (NSString *) mensagem : (NSString *) cancelButton : (NSString *) otherButton {

    UIAlertView * alertView = nil;

    if (otherButton) {

        alertView = [[UIAlertView alloc]
                     initWithTitle:titulo
                     message:mensagem 
                     delegate:self cancelButtonTitle:cancelButton
                     otherButtonTitles:otherButton, nil ];

    } else {

        alertView = [[UIAlertView alloc]
                     initWithTitle:titulo
                     message:mensagem 
                     delegate:self cancelButtonTitle:cancelButton
                     otherButtonTitles:nil ];
    }

    [alertView show];
    [alertView release];

}

Here is the project, if you wanna try for yourself on instruments... http://www.mediafire.com/download.php?hml2hl5laz9ez2j

How do I solve that?

thanks.

A: 

Do you still get the memory errors if you autorelease the UIAlert? So, changing it to:

alertView = [[[UIAlertView alloc]
                 initWithTitle:titulo
                 message:mensagem 
                 delegate:self cancelButtonTitle:cancelButton
                 otherButtonTitles:otherButton, nil ] autorelease;

You should then also remove the [alertView release]; line.

But you're right, no obvious memory error here that I can see.

h4xxr
it still leaks even if autoreleased... amazing.
Digital Robot
+1  A: 

I tried to run it myself in Instruments, and I do not get these leaks. There must be something wrong with the other parts of your code, but this seems fine to me.

Only thing I'll pick on is the way you run showAlert:, [self showAlert: myTitle : message: cancelButton : otherTitles];. I think it quite ugly, you should change the function to

- (void) showAlertWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)cancelButton otherButton:(NSString *)otherButton

and run it with [self showAlertWithTitle:title message:message cancelButton:cancelButton otherButton:otherButton];.

Emil
yes, it is ugly. I have to simplify my code to post here, so it became bare minimum... :)
Digital Robot
Hehe, okey. :) ~
Emil
What version of iOS are you running this on when you get leaks? I don't get any leaks when running your test-project in iOS 4.2..
Emil
iOS 4.2 and it is leaking on my xcode.
Digital Robot
That's really odd..
Emil
+1  A: 

There seems nothing wrong with the code, and even I have figured out some times completely correct code also shows some leaks, now as far as I was reading somewhere, even using profiling tools can show you some leaks because tool itself holds on to variable in case it may be used later on.

So the best way is to initialize everything to autorelease, and use synthesized properties always in order to avoid leaks.

In case if you are using autorelease local variables and only synthesized properties to hold your long term instances, then profiler knows that it doesnt need to hold on to variables.

Akash Kava
it is still leaking even autoreleased. Please download the source and try for yourself. I think this may be some sort of framework leak.
Digital Robot
Yes framework leaks are also there and they are too much, I think profiler can not distinguish between your and framework's leaks or it needs more debugging. But yes I have seen it happening.
Akash Kava
+1  A: 

So, it seems to be a framework leak. I am reporting this to Apple.

Digital Robot