문제

ListController 파일에 상점 목록이 있습니다. 60 개의 상점을 저장 한 SQLITE DB를 설정했습니다. 목록의 상단에는 검색 창이 있습니다.

DAController라는 클래스를 만들었습니다. DB 데이터를로드하고 저장해야합니다.

@interface DataController : NSObject {
 sqlite3 *database;
 NSArray *shops;    
 NSDictionary* dictionaryOfShops;
}

@property (nonatomic, retain) NSDictionary *dictionaryOfShops;
@property (nonatomic, retain) NSArray* shops;
-(void)initializeShops;

Initializeshops 메소드는 DB의 데이터를로드하고 이러한 방식으로 2 개의 소품으로 결과를 저장합니다.

-(void)initializeShops{
    [dictionaryOfShops release];
    [shops release];

    NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];

    if (sqlite3_open(....))
    NSString *query = ....
    if (sqlite3_prepare_v2(database, [query UTF8String],-1, &statement, nil) ==   SQLITE_OK) 
    {
        while (sqlite3_step(statement) == SQLITE_ROW) {

           int rId = sqlite3_column_int(statement, 0);
           char *rName = (char *)sqlite3_column_text(statement, 1);

           Shop* s  = [[Shop alloc] init];
           s.ID = rId;
           if(sName != nil) s.Name = [NSString stringWithUTF8String:rName];

               NSString *shopID = [[NSString alloc] initWithFormat:@"%d",s.ID];
           [dictionary setObject:s forKey:shopID];
           [shopID release];
           [s release];         
        }
        sqlite3_finalize(statement);
    }
    [query release];

    dictionaryOfShops = [[NSDictionary alloc] initWithDictionary:dictionary];
    shops =    [[NSArray alloc] initWithArray:[dictionary allValues]];

    dictionary = nil;
    [dictionary release];

       //Sorting
       NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
    NSArray *sortedList =[self.shops sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    self.shops = sortedList; 
    [sort release];
}

문제는 사용자가 검색 표시 줄에 텍스트를 입력 할 때 쿼리 값을 변경 한 다음 (...)를 추가 한 다음 Initializeshops 메소드를 다시 호출한다는 것입니다. 이 두 번째 시간은 너무 많은 누출 (상점 클래스 속성과 관련하여)을 만들고 누출은 또한 nsdictionary와 nsarray입니다.

이것을 당신에게 게시하기 전에 나는 다른 솔루션을 시도했지만 적어도 처음에 initilizeshops라고 부를 때는 누출되지 않습니다.

나는 정말로 그것에 붙어 있기 때문에 어떤 제안도 받아들입니다.

더:

정말 이상한 것은 내 var 사전과 2 개의 소품 상점과 사전 사전에 대한 메모리 관리입니다. 이 코드로

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//add data to dictionary 
dictionaryOfShops = [[NSDictionary alloc] initWithDictionary:dictionary];
shops =    [[NSArray alloc] initWithArray:[dictionary allValues]];
[dictionary release]

Dictionaryofshops와 Shop이 합성 된 두 가지 속성 (비 원자, 유지)이라는 점을 고려하면 누출없이 가치를 어떻게 변경할 수 있습니까?

이 방법을 처음 통과 할 때 아무것도 누출되지 않습니다. 두 번째로부터 너무 많은 물체 (컬렉션의 내용)가 새기 시작합니다.

도움이 되었습니까?

해결책 3

좋아, 오류를 찾았습니다. 수업 상점에서 나는 그 방법을 구현하지 않았다는 것을 알고 있습니다.

 -(void)dealloc

따라서 이전 사전을 공개 할 때 (새로운 과제를 준비하기 위해) 내부의 모든 필드는 출시되지 않았습니다.

다른 팁

첫 번째 질문은입니다 핵심 데이터 만 사용하지 않는 이유는 무엇입니까? 더 빨라질 가능성이 높고 코드가 줄어들고 시간이 지남에 따라 유지하기가 훨씬 쉬울 것입니다. 무딘; Sqlite는 기만적으로 어렵습니다. 시작하기 쉽고 제대로하기가 매우 어렵습니다.

어쨌든 메모리 관리 dictionary 잘못되었습니다. Kennytm이 제안한대로 NIL 과제의 순서를 교체하고 릴리스했기 때문에 충돌하지 않습니다. 내가 제안 할게 ~ 아니다 자동 조정 사전 생성.

그렇지 않으면, 쓰여진 코드는 언뜻보기에 꽤 누출이없는 것 같습니다. 그래서:

  1. 더 많은 코드를 제공 할 수 있습니까? 다른 곳에서 진행되는 흥미로운 기억이 현명한 것이 있습니까?

  2. 스레딩을 전혀 사용하고 있습니까 (또는 nsoperationqueue)?

  3. 누출 기기 아래에서 실행하고 유출되는 특정 물체의 할당의 뒤쪽을 검색 했습니까?

dictionary = nil;
[dictionary release];

이 두 가지 진술을 교체하십시오. 이 형태로 그것은 의미합니다 [nil release] No-OP입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top