2010-12-22 192 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如何聲明。