2012-06-13 45 views
0

我目前正在通過一些內存泄漏進行對抗,並且遇到了一些嚴重的問題,並且還沒有解決我離開的最後一個問題。泄漏儀器出於各種不同原因顯示幾個泄漏都來自相同的方法,主要歸因於NSCFString,NSMutableArray和我稱爲GraphData的類。我試圖用幾種不同的方法解決這個問題,所以希望能夠解決這個問題,希望這個問題很簡單,我忽略了。內存泄露sqlite iOS應用程序

下面是一些代碼:

// the offending, leaking method 
-(NSMutableArray*)fillDataInArray:(NSInteger)keyphrase_id{ 

    NSLog(@"Keyphrase_id:%d", keyphrase_id); 

    NSDate *startdate = [self getDateForApplicationInstalled]; 
    NSDate *enddate = [NSDate date]; 

    NSString *dateString1=[[NSString alloc] initWithString: [fmt stringFromDate:startdate]]; 
    NSString *dateString2=[[NSString alloc] initWithString: [fmt stringFromDate:enddate]]; 

    NSMutableArray *newDataNew = [[NSMutableArray alloc]init]; 
    self.newData = newDataNew; 
    [newDataNew release]; 

    selStmt = nil; 

    if (!selStmt) 
    { 
     const char *sql = "select distinct position, key_time from ranking where keyphrase_id = ? and key_time between ? and ? order by key_time"; 

     if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK) 
     { 
      selStmt = nil; 
     } 

     NSInteger n = keyphrase_id; 
     sqlite3_bind_int(selStmt, 1, n); 

     sqlite3_bind_text(selStmt, 2, [dateString1 UTF8String] , -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(selStmt, 3, [dateString2 UTF8String] , -1, SQLITE_TRANSIENT); 

     NSLog(@"SQL query is: [%s]", sql); 
    } 
    if (!selStmt) 
    { 
     NSAssert1(0, @"Can't build SQL to read keyphrases [%s]", sqlite3_errmsg(database)); 
    } 

    int ret; 

    while ((ret=sqlite3_step(selStmt))==SQLITE_ROW) 
    { 
     GraphData *item = [[GraphData alloc]init]; 

     item.key = sqlite3_column_int(selStmt, 0); 
     item.value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selStmt,1)]; 

     [newData addObject:item]; 

     [item release], item = nil; 
    } 

    sqlite3_reset(selStmt); // reset (unbind) statement 

    [dateString2 release]; 
    [dateString1 release]; 

    return newData; 
} 

//GraphData.h 
@interface GraphData : NSObject{ 
    NSInteger key; 
    NSString *value; 
} 

@property (nonatomic, readwrite) NSInteger key; 
@property (nonatomic, retain) NSString *value; 

-(id)initWithPrimaryKey:(NSInteger) xid; 
-(id)initWithName:(NSString *)n key:(NSInteger)i; 

@end 

//GraphData.m 
#import "GraphData.h" 

@implementation GraphData 

@synthesize key,value; 

-(id)initWithPrimaryKey:(NSInteger) xid{ 

    self.key = xid; 
    self.value = @""; 

    return self; 

} 
-(id)initWithName:(NSString *)n key:(NSInteger)i{ 

    self.key = 0; 
    self.value = n; 

    return self; 

} 
-(void)dealloc{ 


    [value release], value = nil; 
    [super dealloc]; 

} 

@end 

感謝您看我的帖子!

回答

0

泄漏工具告訴你泄漏的物體在哪裏已創建。由於NSCFString,NSMutableArray和GraphData對象從這個方法泄露,讓我們來看看如何發生這種情況。

您只能在NSMutableArray中插入GraphData對象(包含字符串對象),並且它們似乎被正確釋放。因此,要泄漏在此方法內創建的GraphData對象,包含元素的數組必須是泄漏。

請檢查方法的調用者。我假設其中一個正在保留(而不是釋放)該方法的返回值。

此外,您的初始化程序必須更改爲調用super的init,但這與泄漏無關。一個例子:

-(id)initWithPrimaryKey:(NSInteger) xid 
{ 
    self = [super init]; 
    if (self) { 
     self.key = xid; 
     self.value = @""; 
    } 
    return self; 
} 
+0

該方法的調用者似乎保留了他們的方法調用的返回值,所以希望你已經用該答案擊中了頭部的指甲!不過,我現在要回家以後要明天檢查一下。雖然謝謝! – Jace