2010-12-22 249 views
1
while(sqlite3_step(selectstmt) == SQLITE_ROW) { 
      NSInteger primaryKey = sqlite3_column_int(selectstmt, 0); 
//Expenseentry* temp=[[[Expenseentry alloc]init]autorelease]; 
//Expenseentry* temp=[[Expenseentry alloc]init]; 
      temp=nil; 
      temp=[[Expenseentry alloc]init]; 
         //memory leak here 
      temp.ID=[NSString stringWithFormat:@"%d",primaryKey]; 
         //memory leak here 
      int i=1;  
@try{ 
//Expenseentry* temp=[[Expenseentry alloc]init]; 
//tried this but no luck 
NSString *s=[[NSString alloc]initWithFormat:@"%f",sqlite3_column_double(selectstmt, 1)]; 
         temp.amount=s; 
         [s release]; 
         [arrreturn addObject:temp]; 
         //[temp release]; 
//if i uncomment this app crashes 
         //[formatter release]; 
         //printf("\n daata count %d ",[arrreturn count]); 
        } 
        @catch(id ex) 
        { 
         printf("ooooopssss exception "); 
        } 
        i++; 
      } 

 

我的费用入门级内存泄漏


@interface Expenseentry : NSObject { 
    NSString *ID; 
    NSString *amount; 

} 
@property (nonatomic, retain) NSString *ID; 
@property (nonatomic, retain) NSString *amount; 
@end 
and .m is just 
- (void)dealloc { 
    [ID release]; 
[amount release] 
} 
+0

您需要修改的代码,因为它被切断,你可能需要一个更详细的问题,如您认为可能会发生泄漏(使用分析器尝试内置到Xcode中,Cmd + shift + A) – 2010-12-22 13:49:27

+0

@jonathan代码现在已修复。当我第一次运行它时会起作用,但第二次运行时会显示内存泄漏。这是显而易见的原因,因为我没有释放临时原因,如果我将应用程序崩溃 – saurabh 2010-12-22 13:53:14

+0

我已经测试了上面的代码仪器和书面评论声明只是在波纹管它显示内存泄漏 – saurabh 2010-12-22 13:56:04

回答

0

好吧,我发现我的错误只是发表如果任何人能解释这种现象:
内存泄漏的原因阵列和阵列对象不released.If我将释放它的任何应用程序崩溃。

错误1:[super dealloc]在expenseentry的dealloc中丢失。
怀疑:为什么需要发布超级?当苹果文件说你必须释放你拥有的物体。

错误2:此函数返回的数组存储在调用者的实例变量(以及retain为属性的综合属性)中。
我在dealloc中释放了该属性,因为它被保留。

receivedArr=fun()

中的dealloc

[receivedArr release]
2
  1. 看起来温度是该类的实例变量
  2. 请确认您发布的临时当您完成或在你面前的权利再次使用它

尝试做以下操作

[temp release]; 
temp=[[Expenseentry alloc]init]; 
temp.ID=[NSString stringWithFormat:@"%d",primaryKey]; 

另一种选择是,如果temp.ID串泄漏你需要寻找到Expenseentry类,以确保它你做了一段时间(sqlite3_step)环

while(sqlite3_step(selectstmt) == SQLITE_ROW) { 
... 
temp=[[Expenseentry alloc]init]; 
temp.ID=[NSString stringWithFormat:@"%d",primaryKey]; 
... //Use temp 
[temp release]; 
temp = nil; //Best practice to set it to nil 

内后释放你在那里做适当的内存管理。

编辑:我现在看到你的代码的其余部分贴

[arrreturn addObject:temp]; 
//[temp release]; 
//if i uncomment this app crashes 

它为什么可能崩溃的原因是因为我以前说过确保将其释放

编辑2后设置为nil:您正在while循环中重复使用同一个对象 您需要将临时分配移动到while循环中,否则该数组中的每个对象都将指向同一个对象。我不确定你的目标是什么,但看看下面的代码。

while(i>5) 
{ 
    temp=[[Expenseentry alloc]init]; 
    temp.ID=[NSString stringWithFormat:@"%d",primaryKey]; 
    @try 
    { 
     NSString *s=[[NSString alloc]initWithFormat:@"%f",sqlite3_column_double(selectstmt, 1)]; 
         temp.amount=s; 
         [s release]; 
         [arrreturn addObject:temp]; 
    } 
    @catch(id ex) 
    { 
     printf("ooooopssss exception "); 
    } 
    [temp release]; 
    temp = nil; 
    i++; 
} 
1

temp = nil似乎有点奇怪。无论何时将变量temp分配给新对象,都不要忘记释放前一个对象。

,如果你写:

Expenseentry* temp=[[Expenseentry alloc]init]; 
temp=nil; 

你会得到一个内存泄漏,因为你已经创建了一个Expenseentry对象,然后总算摆脱了那里的对象主要。你需要做一个[临时发布];在iphone上分配给nil之前。

在您的Expenseentry中可能会出现其他泄漏,但您并未显示它是如何看起来的,即属性ID如何声明。