新手泄漏我不太明白

时间:2010-08-18 12:41:45

标签: iphone memory-leaks

在修复了仪器指示的其他泄漏之后,我发现自己需要帮助找到以下代码中的浸出物:

  

- (void)readHistoryFromDatabase {       sqlite3 *数据库;

NSMutableArray *days = [[NSMutableArray alloc] init];

NSInteger currentMonth;

int noMonths = 0;

int historyCount = 0;

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"database.sqlite"];

if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) { 
    const char *sqlStatement = "select data, rata, var, strftime('%m', data) month from rates where id=?"; 
    sqlite3_stmt *compiledStatement; 

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        sqlite3_bind_int(compiledStatement, 1, id);
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            NSDate   *data      = [dateFormat dateFromString:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 0)]];
            NSString *rata      = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
            NSString *var       = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];
            NSInteger month     = sqlite3_column_int(compiledStatement, 3);

            if ((currentMonth != month) && (historyCount > 0)) {
                [self.rate setValue:[days copy] forKey:[NSString stringWithFormat:@"%d", noMonths]];

                noMonths++;
                [days removeAllObjects];
                currentMonth = month;
            } else if (historyCount == 0) {
                currentMonth = month;
            }

            CHis *new = [[CHis alloc] init]; 
            new.rata  = rata;
            new.var   = variatie;
            new.month = month;
            new.data  = data;
            historyCount++;
            [days addObject:new];
            [new release];
        }
        if ([days count] > 0) {
            [self.rate setValue:[days copy] forKey:[NSString stringWithFormat:@"%d", noMonths]];
        }
    }
    sqlite3_finalize(compiledStatement);
}
[dateFormat release];
[days release];
sqlite3_close(database);

}

self.rate以这种方式定义:

  

@property(nonatomic,retain)NSMutableDictionary * rate;

并在viewDidLoad中:

  

self.rate = [[NSMutableDictionary alloc] init];

基本上这部分代码从数据库读取一些费率。根据月份,它将在NSDictionary中放入一对月份和一个带有费率的数组。

我不明白泄漏在哪里。我知道也许这很容易,但我现在正在寻找解决方案2天......

2 个答案:

答案 0 :(得分:2)

[[NSMutableDictionary alloc] init]返回一个保留计数为1的对象。然后将其分配给self.rate,这会增加保留计数(然后将为2)。如果您将其他内容分配给self.rate,则上一个对象的保留计数将回落到1.但它不会达到0.

有三种解决方案:

// OK
self.rate = [[[NSMutableDictionary alloc] init] autorelease];

// Better, has the same effect as statement above
self.rate = [NSMutableDictionary dictionary];

// Clumsy, but works
NSMutableDictionary *tmp = [[NSMutableDictionary alloc] init];
self.rate = tmp;
[tmp release];

请记住,每次拨打alloc/initcopy时,都会获得您拥有的对象。这意味着您有责任在其上调用release。几乎所有其他方法都将/应该返回一个您不需要调用release的对象,除非您在其上调用retained以“声明所有权”。

答案 1 :(得分:0)

在仪器中,您可以看到泄漏的来源,按下扩展的详细信息按钮并选择泄漏,然后您将看到泄漏源自哪种方法和文件。