NSString丢失参考

时间:2012-04-17 23:11:32

标签: objective-c ios memory-management

我的内存管理有问题。我有一个类来分配要在缓存中使用的数据:

+ (void) getInitialDataToDisplay:(NSString *)dbPath {

    iMapAppDelegate *appDelegate = (iMapAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sql = "SELECT COD, ARTRF2, DESCRI, APLICA, PRV1, PICMENT, UM, DESMAX, QATU_01, QATU_11, QATU_12, QATU_13, PRPROM, GRUPO, EMBAP, IMGAP, R_E_C_N_O_ FROM APSB1010 ORDER BY COD;";
        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSInteger primaryKey = sqlite3_column_int(selectstmt, 16);

                iMapProdutos *prodObj = [[iMapProdutos alloc] initWithPrimaryKey:primaryKey];

                prodObj.prodCod = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
                prodObj.prodArtrf2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                prodObj.prodDescri = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
                prodObj.prodAplic = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
                prodObj.prodPrv1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)]; 
                prodObj.prodPicment = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
                prodObj.prodUm = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)]; 
                prodObj.prodDesMax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
                prodObj.prodQAtu_01 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 8)];
                prodObj.prodQAtu_11 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 9)];
                prodObj.prodQAtu_12 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 10)];
                prodObj.prodQAtu_13 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 11)];
                prodObj.prodPrProm = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 12)];
                prodObj.prodGrup = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 13)];
                prodObj.prodEmbAP = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 14)];
                prodObj.prodImgAP = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 15)];

                prodObj.isDirty = NO;

                [appDelegate.prodArray addObject:prodObj];
                [prodObj release];
            }
        }
    }
    else
        sqlite3_close(database);
}

我需要清空缓存,但不能丢失您的引用。应该怎么做,我现在正在尝试这个:

iMapAppDelegate *appDelegate = (iMapAppDelegate *) [[UIApplication sharedApplication] delegate];

    for (int i = 0; i < [appDelegate.prodArray count]; i++) {

        iMapProdutos *prod = [appDelegate.prodArray objectAtIndex:i];

        [prod.prodCod release]; 
        [prod.prodArtrf2 release]; 
        [prod.prodDescri release];
        [prod.prodAplic release];
        [prod.prodPrv1 release]; 
        [prod.prodPicment release]; 
        [prod.prodUm release]; 
        [prod.prodDesMax release]; 
        [prod.prodQAtu_01 release]; 
        [prod.prodQAtu_11 release]; 
        [prod.prodQAtu_12 release]; 
        [prod.prodQAtu_13 release]; 
        [prod.prodPrProm release];
        [prod.prodGrup release];
        [prod.prodEmbAP release];
        [prod.prodImgAP release];
    }

0 个答案:

没有答案