Iphone 正在释放变量,但应用程序仍在泄漏内存并崩溃

Iphone 正在释放变量,但应用程序仍在泄漏内存并崩溃,iphone,xcode,memory,memory-management,memory-leaks,Iphone,Xcode,Memory,Memory Management,Memory Leaks,当我的应用程序第一次加载时,它会将多个文本文件中的数据加载到SQLite数据库中。有很多数据,所以我将文件分割成几个较小的文件,并使用下面的代码在循环中加载数据。我在每次传递时释放每个变量,并写入NSLog以记录当时的内存状态。即使我正在释放变量,内存正在减少,我收到内存警告,级别1和2,然后应用程序关闭。是否向SQLite表中添加数据会导致内存减少,或者我在代码中遗漏了什么 while (filePathNo != @"7") { NSString *filePath; NSStrin

当我的应用程序第一次加载时,它会将多个文本文件中的数据加载到SQLite数据库中。有很多数据,所以我将文件分割成几个较小的文件,并使用下面的代码在循环中加载数据。我在每次传递时释放每个变量,并写入NSLog以记录当时的内存状态。即使我正在释放变量,内存正在减少,我收到内存警告,级别1和2,然后应用程序关闭。是否向SQLite表中添加数据会导致内存减少,或者我在代码中遗漏了什么

while (filePathNo != @"7") {
  NSString *filePath;
  NSString *GenericName = nil; 
      .
      .
      // Nineteen Other Variables
      .
      .
  if (filePathNo == @"1") {
        filePath = [[NSBundle mainBundle] pathForResource:@"FileLoad1" ofType:@"txt"];
        filePathNo = @"2";
  }
      else if (filePathNo == @"2") {
        filePath = [[NSBundle mainBundle] pathForResource:@"FileLoad2" ofType:@"txt"];
        filePathNo = @"3";
  }
      .
      .
      // Five Other Files declared   
  .
      .

  NSString *textFromFile = [NSString stringWithContentsOfFile:filePath];

  NSString *cellString = textFromFile;
  NSRange range2 = [cellString rangeOfString:@"<string>"];
  range2 = [cellString rangeOfString:@"<string>"];
  if (range2.location != NSNotFound ) {
      eof = @"N";
      cellString = [cellString substringFromIndex:range2.location + 8];
  }

  while (eof != @"Y") {
    NSLog(@"Drug is - %@ , Memory free is  %d", GenericName, [self get_freemem]);
    progVal = progval + 1;                      
    [self performSelectorOnMainThread:@selector(updateMyProgressBar) withObject:nil waitUntilDone:NO];
    NSRange range1 = [cellString rangeOfString:@"#"];
    if (range1.location != NSNotFound )  
        GenericName = [cellString substringToIndex:range1.location]; 
    cellString = [cellString substringFromIndex:range1.location + 1];
            .
            .
            //  Find and load nineteen other variables
            .
            .
            range2 = [cellString rangeOfString:@"</string>"];
        if (range2.location != NSNotFound )            
        Doses = [cellString substringToIndex:range2.location];
    cellString = [cellString substringFromIndex:range2.location + 9];
    range2 = [cellString rangeOfString:@"<string>"];
    if (range2.location != 0 ) {
        eof = @"N";
        @try {
            cellString = [cellString substringFromIndex:range2.location + 8];
        }
        @catch (NSException *exception) {
            eof = @"Y";
        }
    }
    else {
        eof = @"Y";
    }
    if (cellString == nil) {
        eof = @"Y";
    }

    NSString *spkrs = @"";  
    char *errorMsg;
    char *update = "INSERT OR REPLACE INTO DRUGTABLE (FullDrugName, GenericName, OtherNames, TradeName, PrescriptionStatus, Formulations, DrugAction, DrugUse, SafetyAndHandling, Contraindications, AdverseReactions, DrugInteractions, Therapeuticgroup, GeneralAction, SpecificAction, ChemicalGroup, DrugReferences, Furtherreading, Doses) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    //int errVal = sqlite3_prepare_v2(database, update, -1, &stmt, nil);
    if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK);
    {

        sqlite3_bind_text(stmt, 1, [FullDrugName  UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 2, [GenericName UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 3, [OtherNames UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 4, [TradeName UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 5, [PrescriptionStatus UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 6, [Formulations UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 7, [DrugAction UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 8, [DrugUse UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 9, [SafetyAndHandling UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 10, [Contraindications UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 11, [AdverseReactions UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 12, [DrugInteractions UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 13, [Therapeuticgroup UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 14, [GeneralAction UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 15, [SpecificAction UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 16, [ChemicalGroup UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 17, [References UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 18, [FurtherReading UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 19, [Doses UTF8String], -1, NULL);
    }
    if (sqlite3_step(stmt) != SQLITE_DONE)
    {
        NSString *err = errorMsg;
    }
    sqlite3_finalize(stmt);
    [spkrs release];
  } 
  [GenericName release]; 
  .
      .
      // Release all nineteen other variables
      .
      .
}
while(filePathNo!=@“7”){
NSString*文件路径;
NSString*GenericName=nil;
.
.
//其他19个变量
.
.
如果(filePathNo=@“1”){
filePath=[[NSBundle mainBundle]路径用于资源:@“txt”类型的“FileLoad1”;
filePathNo=@“2”;
}
else if(filePathNo=@“2”){
filePath=[[NSBundle mainBundle]路径用于资源:@“txt”类型的“FileLoad2”;
filePathNo=@“3”;
}
.
.
//声明了其他五个文件
.
.
NSString*textFromFile=[NSString stringWithContentsOfFile:filePath];
NSString*cellString=textFromFile;
NSRange range 2=[cellString rangeOfString:@”“];
range2=[cellString rangeOfString:@”“];
if(range2.location!=NSNotFound){
eof=@“N”;
cellString=[cellString substringFromIndex:range2.location+8];
}
而(eof!=@“Y”){
NSLog(@“药物为-%@,无内存为%d”,通用名,[self get_freemem]);
progVal=progVal+1;
[self-performSelectorOnMainThread:@selector(updateMyProgressBar),对象:nil waitUntilDone:NO];
NSRange range1=[cellString rangeOfString:@“#”];
if(range1.location!=NSNotFound)
GenericName=[cellString substringToIndex:range1.location];
cellString=[cellString substringFromIndex:range1.location+1];
.
.
//查找并加载十九个其他变量
.
.
range2=[cellString rangeOfString:@”“];
if(range2.location!=NSNotFound)
剂量=[单元格字符串子字符串到索引:范围2.位置];
cellString=[cellString substringFromIndex:range2.location+9];
range2=[cellString rangeOfString:@”“];
如果(范围2.location!=0){
eof=@“N”;
@试一试{
cellString=[cellString substringFromIndex:range2.location+8];
}
@捕获(NSException*异常){
eof=@“Y”;
}
}
否则{
eof=@“Y”;
}
if(cellString==nil){
eof=@“Y”;
}
NSString*spkrs=@;
char*errorMsg;
char*update=“在药物表中插入或替换(完整药物名称、通用名称、其他名称、商品名、处方状态、配方、药物作用、药物使用、安全性和处理、禁忌症、不良反应、药物相互作用、治疗组、通用作用、特异性作用、化学组、药物参考、进一步阅读、剂量)值(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
//int errVal=sqlite3\u prepare\u v2(数据库,更新,-1,&stmt,无);
if(sqlite3\u prepare\u v2(数据库,更新,-1,&stmt,nil)=SQLITE\u OK);
{
sqlite3\u bind\u text(stmt,1,[FullDrugName UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,2,[GenericName UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,3,[OtherNames UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,4,[TradeName UTF8String],-1,NULL);
sqlite3绑定文本(stmt,5,[PrescriptionStatus UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,6,[UTF8String],-1,NULL);
sqlite3_bind_text(stmt,7,[DrugAction UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,8,[DrugUse UTF8String],-1,NULL);
sqlite3_bind_text(stmt,9,[SafetyAndHandling UTF8String],-1,NULL);
sqlite3绑定文本(stmt,10,[禁忌症UTF8String],-1,空);
sqlite3\u bind\u text(stmt,11,[DisposterActions UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,12,[UTF8String],-1,NULL);
sqlite3绑定文本(stmt,13,[Therapeutical Group UTF8String],-1,NULL);
sqlite3_bind_text(stmt,14,[GeneralAction UTF8String],-1,NULL);
sqlite3_bind_text(stmt,15,[SpecificAction UTF8String],-1,NULL);
sqlite3绑定文本(stmt,16,[ChemicalGroup UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,17,[References UTF8String],-1,NULL);
sqlite3_bind_text(stmt,18,[进一步读取UTF8String],-1,NULL);
sqlite3\u bind\u text(stmt,19,[UTF8String],-1,NULL);
}
如果(sqlite3_步骤(stmt)!=SQLITE_完成)
{
NSString*err=errorMsg;
}
sqlite3_最终确定(stmt);
[spkrs发布];
} 
[通用名称发布];
.
.
//释放所有其他十九个变量
.
.
}

除了上次的
[spkrs release]
[GenericName release]
之外,我在这段代码中看不到任何发布调用

您是否尝试使用Xcode附带的探查器构建项目?它可以为您提供大量有用的信息,说明哪些对象正在泄漏,泄漏的位置和原因


使用Run->Start with Performance Tool->Leaks尝试一下,我认为你应该做的是在你的循环之外放置一个
NSAutoReleasePool

drain
it。您可能正在循环中创建充当泄漏的自动释放对象。例如
cellString=[cellString substringFromIndex:range1.location+1]

你不应该释放
GenericString
spkrs
,因为你从不
alloc
保留它们。事实上,我不确定你为什么会有
spkrs
变量,因为它看起来不像你在任何事情上使用它。

试着使用工具来避免它