Dans mon application, un lecteur RSS, je reçois des fuites de mémoire que je ne peux pas réparer parce que je ne comprends pas d'où ils viennent. Voici le code signalé par Instruments.iPhone - méthode de lecture de base de données et les fuites de mémoire
-(void) readArticlesFromDatabase {
[self setDatabaseInfo];
sqlite3 *database;
articles = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from articles";
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *aAuthor = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSString *aSummary = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
NSMutableString *aContent = [NSMutableString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
NSString *aNbrComments = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
NSString *aCommentsLink = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
NSString *aPermalink = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 11)];
[aContent replaceCharactersInRange: [aContent rangeOfString: @"http://www.mywebsite.com/img/action-on.gif"] withString: @"hellocoton-action-on.gif"];
[aContent replaceCharactersInRange: [aContent rangeOfString: @"hhttp://www.mywebsite.com/img/action-on-h.gif"] withString: @"hellocoton-action-on-h.gif"];
[aContent replaceCharactersInRange: [aContent rangeOfString: @"hthttp://www.mywebsite.com/img/hellocoton.gif"] withString: @"hellocoton-hellocoton.gif"];
NSString *imageURLBrut = [self parseArticleForImages:aContent];
NSString *imageURLCache = [imageURLBrut stringByReplacingOccurrencesOfString:@":" withString:@"_"];
imageURLCache = [imageURLCache stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
imageURLCache = [imageURLCache stringByReplacingOccurrencesOfString:@" " withString:@"_"];
NSString *uniquePath = [tmp stringByAppendingPathComponent: imageURLCache];
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) {
imageURLCache = [@"../tmp/" stringByAppendingString: imageURLCache];
[aContent replaceCharactersInRange: [aContent rangeOfString: imageURLBrut ] withString: imageURLCache];
}
Article *article = [[Article alloc] initWithName:aName date:aDate url:aUrl category:aCategory author:aAuthor summary:aSummary content:aContent commentsNbr:aNbrComments commentsLink:aCommentsLink commentsRSS:@"" enclosure:aPermalink enclosure2:@"" enclosure3:@""];
[articles addObject:article];
article = nil;
[article release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
`
J'ai beaucoup de "article" et NSString fuite correspondant à ces utilisant:
[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, X)];
J'ai essayé beaucoup de code différent, je l'ai toujours ces fuites. Quelqu'un a une idée pour m'aider?
Nous vous remercions de votre réponse. J'ai essayé d'ajouter cela parce que je travaille sur ce point depuis longtemps maintenant et je suis à la recherche de cela. Comme vous l'avez dit le problème ne vient pas de ce point (qui ne change rien (avec ou sans) selon les Instruments). Vous avez dit qu'il y a beaucoup plus de problèmes dans ce code, avez-vous une idée de la raison pour laquelle ces lignes créent des fuites: NSString * aName = [NSString stringWithUTF8String: (char *) sqlite3_column_text (compiledStatement, 1)]; Nous vous remercions de votre temps et de votre aide. – Do8821
Supprimer l'affectation à zéro arrêtera vos articles qui fuient. –
Vous faites également une fuite sur le tableau "articles", et vous devriez le publier à la fin de la méthode. Si vous souhaitez le conserver en tant que variable d'instance, vous devez utiliser une attribution de propriété. –