从 Sqlite 获取数据时内存泄漏
我在下面的代码中遇到内存泄漏。
这是处理内存的正确方法吗?
请纠正我。
-(void)getholidays
{
if (idarray!=nil) {
[idarray release];
idarray=nil;
}
idarray=[[NSMutableArray alloc]init];
if (Countryarray!=nil) {
[Countryarray release];
Countryarray=nil;
}
Countryarray =[[NSMutableArray alloc] init];
if (Holidaynamearray!=nil) {
[Holidaynamearray release];
Holidaynamearray=nil;
}
Holidaynamearray =[[NSMutableArray alloc] init];
if (Datearray!=nil) {
[Datearray release];
Datearray=nil;
}Datearray =[[NSMutableArray alloc] init];
if (Dayarray!=nil) {
[Dayarray release];
Dayarray=nil;
}Dayarray =[[NSMutableArray alloc] init];
if (Favoritearray!=nil) {
[Favoritearray release];
Favoritearray=nil;
}
Favoritearray =[[NSMutableArray alloc] init];
NSString *destinationPath = [self getdestinationPath];
const char *dbpath = [destinationPath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL;
NSDate *today = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMMM-dd-yyyy"];
NSString *Todaystrng = [formatter stringFromDate:today];
NSLog(@"today date %@",Todaystrng);
querySQL=[NSString stringWithFormat:@"SELECT * FROM Holiday_Table WHERE CountryName in (SELECT Country_Name from Country WHERE Country_Selected =1) ORDER BY Date ASC "];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"success");
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *idstringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSString *countrynamestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
NSString *holidaynamestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
NSString *datestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
//Below line gets leaking
NSString *daystringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
//Belowline gets leaking
NSString *favoritestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
[idarray addObject:idstringfromdb];
[idstringfromdb release];
idstringfromdb=nil;
[Countryarray addObject:countrynamestringfromdb];
[countrynamestringfromdb release];
countrynamestringfromdb=nil;
[Holidaynamearray addObject:holidaynamestringfromdb];
[holidaynamestringfromdb release];
holidaynamestringfromdb=nil;
[Datearray addObject:datestringfromdb];
[datestringfromdb release];
datestringfromdb=nil;
[Dayarray addObject:daystringfromdb];
[daystringfromdb release];
daystringfromdb=nil;
[Favoritearray addObject:favoritestringfromdb];
[favoritestringfromdb release];
favoritestringfromdb=nil;
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
Iam getting memory leaks in below code.
Is this is the proper way to handle memory?
Please do correct me.
-(void)getholidays
{
if (idarray!=nil) {
[idarray release];
idarray=nil;
}
idarray=[[NSMutableArray alloc]init];
if (Countryarray!=nil) {
[Countryarray release];
Countryarray=nil;
}
Countryarray =[[NSMutableArray alloc] init];
if (Holidaynamearray!=nil) {
[Holidaynamearray release];
Holidaynamearray=nil;
}
Holidaynamearray =[[NSMutableArray alloc] init];
if (Datearray!=nil) {
[Datearray release];
Datearray=nil;
}Datearray =[[NSMutableArray alloc] init];
if (Dayarray!=nil) {
[Dayarray release];
Dayarray=nil;
}Dayarray =[[NSMutableArray alloc] init];
if (Favoritearray!=nil) {
[Favoritearray release];
Favoritearray=nil;
}
Favoritearray =[[NSMutableArray alloc] init];
NSString *destinationPath = [self getdestinationPath];
const char *dbpath = [destinationPath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL;
NSDate *today = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMMM-dd-yyyy"];
NSString *Todaystrng = [formatter stringFromDate:today];
NSLog(@"today date %@",Todaystrng);
querySQL=[NSString stringWithFormat:@"SELECT * FROM Holiday_Table WHERE CountryName in (SELECT Country_Name from Country WHERE Country_Selected =1) ORDER BY Date ASC "];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"success");
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *idstringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSString *countrynamestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
NSString *holidaynamestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
NSString *datestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
//Below line gets leaking
NSString *daystringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
//Belowline gets leaking
NSString *favoritestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
[idarray addObject:idstringfromdb];
[idstringfromdb release];
idstringfromdb=nil;
[Countryarray addObject:countrynamestringfromdb];
[countrynamestringfromdb release];
countrynamestringfromdb=nil;
[Holidaynamearray addObject:holidaynamestringfromdb];
[holidaynamestringfromdb release];
holidaynamestringfromdb=nil;
[Datearray addObject:datestringfromdb];
[datestringfromdb release];
datestringfromdb=nil;
[Dayarray addObject:daystringfromdb];
[daystringfromdb release];
daystringfromdb=nil;
[Favoritearray addObject:favoritestringfromdb];
[favoritestringfromdb release];
favoritestringfromdb=nil;
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我从您的代码中可以看出,泄漏是由于将 addObject 添加到数组(这会将保留计数增加一)而发生的,请确保当您删除对象并从数组中完成使用它时,您也会在那里释放它。
As far as I can tell from your code the leak happens because of addObject to the array (which increases the retain count by one) make sure that when you remove the object and are done with it from the array you release it there as well.