簡體   English   中英

在儀器中查找內存泄漏的原因

[英]Finding the cause of memory leak in Instruments

我已經在Instruments中運行了泄漏,它向我顯示了100%值的內存泄漏。 我能夠看到導致問題的代碼行。 但不是很確定錯誤是什么..

- (void) listAllBooks {
    if (marrListFromDB != nil) {
        [marrListFromDB removeAllObjects];
        marrListFromDB = nil;
    }

    marrListFromDB = [[NSMutableArray alloc] init];
    ServerCommunicationAPI *servApi = [[ServerCommunicationAPI alloc] init];
    servApi.delegate = self;
    NSURL *url = [NSURL URLWithString:kLISTCONTENTS];
    [servApi listBooksWithDeviceID:singleton.g_strdevID deviceKey:singleton.g_strdevID andSessionString:singleton.g_strSessionID sessionKey:@"sessionKey" URL:url andRequestMethod:@"POST"];
}

錯誤線是最后一條。 不知道為什么會導致內存泄漏...需要一些指導。

從所提供的信息很難分辨,但也許ServerCommunicationAPI屬性被聲明為(strong) 在這種情況下, servApi永遠不會被釋放,因為它對自身有很強的引用(保留周期)。
我建議您檢查儀器中哪種對象泄漏,這將使答案容易得多。

試試這個。 可以解決您的內存泄漏問題。

- (void) listAllBooks {
if (marrListFromDB != nil) {
    [marrListFromDB removeAllObjects];
    marrListFromDB = nil;
}
ServerCommunicationAPI *servApi ;
marrListFromDB = [[NSMutableArray alloc] init];
if(servApi == nil){

     ServerCommunicationAPI *servApi = [[ServerCommunicationAPI alloc] init];
}//Every time it going to alloc. It's strong object may be due do this memory leak happens. 
servApi.delegate = self;
NSURL *url = [NSURL URLWithString:kLISTCONTENTS];
[servApi listBooksWithDeviceID:singleton.g_strdevID deviceKey:singleton.g_strdevID andSessionString:singleton.g_strSessionID sessionKey:@"sessionKey" URL:url andRequestMethod:@"POST"];

}

另一個想法:也許您在沒有設置自動釋放池的單獨線程中執行代碼? 在這種情況下,發送到servApi的消息可能會創建自動釋放對象,這些對象以后將無法釋放,因為不存在自動釋放池。
因此,如果您的代碼未在主線程中執行,請檢查是否已使用@autoreleasepool {...}塊為您的線程設置了自動釋放池。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM