Leak on appendFormat
I'm using instruments to detect some leaks and there's some leaks I can't resolve;
NSMutableString *textedetails = [[NSMutableString alloc] init]; ------->2,3%
if([dico objectForKey:@"alertSerie"] != nil)
{[textedetails appendFormat:@"Numéro de Série: %@ \n",[dico objectForKey:@"alertSerie"]];}
if([dico objectForKey:@"alertDate"] != nil)
{[textedetails appendFormat:@"Date de mise en service: %@ \n",[dico objectForKey:@"alertDate"]];}
if([dico objectForKey:@"alertCli"] != nil)
{[textedetails appendFormat:@"Nom du client associé: %@ \n",[dico objectForKey:@"alertCli"]];} ------->20,9%
NSMutableString *texterecap = [[NSMutableString alloc] init];------->2,3%
if([dico objectForKey:@"alertIndex"] != nil)
{[texterecap appendFormat:@"Index du Compteur: %@ \n",[dico objectForKey:@"alertIndex"]];}
if([dico objectForKey:@"alertE2day"] != nil)
{[texterecap appendFormat:@"Energie produite aujourd'hui: %@ \n",[开发者_StackOverflow社区dico objectForKey:@"alertE2day"]];}
if([dico objectForKey:@"alertEmo"] != nil)
{[texterecap appendFormat:@"Energie produite ce mois-ci: %@ \n",[dico objectForKey:@"alertEmo"]];}
if([dico objectForKey:@"alertEy"] != nil)
{[texterecap appendFormat:@"Energie produite cette année-ci: %@ \n",[dico objectForKey:@"alertEy"]];}
if([dico objectForKey:@"alertCO"] != nil)
{[texterecap appendFormat:@"CO2 économisé cette année: %@ \n",[dico objectForKey:@"alertCO"]];}
if([dico objectForKey:@"alertPerfRel"] != nil)
{[texterecap appendFormat:@"Performance relative: %@ \n",[dico objectForKey:@"alertPerfRel"]];} ------->74,4%
[dico release];
details.numberOfLines=0; // Pour mettre le nombre de lignes possibles à infini.
details.text = [NSString stringWithFormat:@"%@", textedetails ];
recapitulatif.numberOfLines=0;
recapitulatif.text = [NSString stringWithFormat:@"%@", texterecap ];
[textedetails release];
[texterecap release];
...
- (void)dealloc {
[login dealloc];
[motdepasse dealloc];
[responseData dealloc];
[super dealloc];
}
And here :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login et Mot de passe" message:@"Votre login et votre mot de passe sont enregistrés."delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];[alert release]; ------> 100%
I don't understand those leaks and howto resolve them!
Thanks to help me :D
EDIT :
Apparently, the leak for the alertview isn't a leak.
For the first leak, there is a leak again !!!
In the first case, it seems to me that you are not releasing two variables: textedetails
and texterecap
(at least, you are not doing it in the code shown). It is not clear what you are doing next with those strings (assigning to properties?), if you show some more code I might be able to help you further.
Your dealloc
method is quite not right; it should be:
- (void)dealloc {
[login release];
[motdepasse release];
[responseData release];
[super dealloc];
}
I don't know if this could also solve what instruments is reporting, since it is pretty unusual calling dealloc
on an object (i.e., I don't know what effects it could have).
Furthermore, it seems that you are not releasing recapitulatif
.
In the second case, the alert view is not being dismissed correctly, I guess. The code is correct; I would only try and implement the delegate protocol to see if the – alertView:willDismissWithButtonIndex:
method is actually called.
In the first example you are alloc/initing two NSMutableStrings without calling release on them. The UIAlertView usage looks ok.
Try using the Analyze function in Xcode. It will point out where the leaks are occurring and explain why.
Why do you really want NSMutuableString
you could have used
NSString *textedetails;;
NSString *texterecap;
if([dico objectForKey:@"alertSerie"] != nil){
[textedetails stringByAppendingFormat:@"Numéro de Série: %@ \n",[dico objectForKey:@"alertSerie"]];
}
if(....){}
details.text = [NSString stringWithFormat:@"%@", textedetails ];
recapitulatif.numberOfLines=0;
recapitulatif.text = [NSString stringWithFormat:@"%@", texterecap ];
** this is autoreleased instance, so you do not need to call explicitly release on them **
you need to release only those object which is created by using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).
and in your second question, if you would have passed the delegate as self, would not be leak i guess. (according to your comment) i think some thing wrong then, because the below code for alert view will never leak.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login et Mot de passe" message:@"Votre login et votre mot de passe sont enregistrés." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
精彩评论