2011-07-07 47 views
1

我在下面的代碼中有內存泄漏。在樂器本身墜毀之前,我得到了87.5%的內存泄漏。也許你們中的一個可以告訴我這段代碼有什麼問題。我需要在這裏發佈任何東西嗎?謝謝。分配在方法之間傳遞的Core Data managedobject時發生內存泄漏

- (void)browseSSviewControllerDidFinish:(browseSSviewController *)controller { 
<SNIP> 
     MANAGED_OBJECT_1 = [self newFormatFromFormat:MANAGED_OBJECT_2]; 
<SNIP> 
} 

- (Format *)newFormatFromFormat:(Format *)formatToCopy { 

    NSLog(@"making new format that's a copy"); 
    Format *thisNewFormat = [self newBlankFormat]; 
    [self updateFormat:thisNewFormat withNumbersFromFormat:formatToCopy]; 
    return thisNewFormat; 
} 

-(void)updateFormat:(Format *)formatToCopyTo withNumbersFromFormat:(Format *)formatToCopyFrom { 
    NSLog(@"copying formats"); 
    formatToCopyTo.x = formatToCopyFrom.x; 
    formatToCopyTo.y = formatToCopyFrom.y; 
    formatToCopyTo.z = formatToCopyFrom.z; 
    formatToCopyTo.a = formatToCopyFrom.a; 
    formatToCopyTo.n = formatToCopyFrom.n; 
    formatToCopyTo.u = formatToCopyFrom.u; 
    formatToCopyTo.s = formatToCopyFrom.s; 
} 

- (Format *)newBlankFormat { 

    NSLog(@"making new blank format"); 
    gfghfAppDelegate *del = (gfghfAppDelegate *)[UIApplication sharedApplication].delegate; 
    NSManagedObjectContext *MOC = del.managedObjectContext; 
    NSPersistentStoreCoordinator *PSC = [MOC persistentStoreCoordinator]; 
    NSManagedObjectModel *MOM = [PSC managedObjectModel]; 
    NSEntityDescription *entity = [[MOM entitiesByName] objectForKey:@"Format"]; 
    Format *thisNewFormat = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:MOC]; 
    thisNewFormat.slot = [NSNumber numberWithInt:-1]; 
    NSLog(@"slot = %@",thisNewFormat.slot); 
    return thisNewFormat; 
} 

這裏是我能儀器之前離開我回溯的泄漏的87.5%墜毀:

+0xc7 calll DYLD-STUB$$objc_msgSend = 87.5%

我沒有使用mutableCopy複製我的MANAGED_OBJECTs因爲它只是沒有工作。

+0

PS - MANAGED_OBJECT_1和MANAGED_OBJECT_2都是實例變量 - 自定義的屬性查看控制器對象。 – CommaToast

回答

0

我改寫了newBlankFormat:thisNewFormat方法和所用的「insertNewObjectForEntityForName:inManagedObjectContext:」 NSEntityDescription的簡便方法(請參閱「創建,初始化和保存管理對象」,在蘋果公司的「創建和刪除管理對象」一章「核心數據編程指南「)。

我認爲我的問題最初是我使用「NSManagedObject alloc」與「initWithEntity:insertIntoManagedObjectContext」,它不會自動發佈的對象。即使當我從另一個方法中釋放它時,它會泄漏,即使用獲取此方法返回值的實例變量,稍後使用「釋放」返回值。但是,無論我是否在malloc中保留「保留」,發佈計數都沒有增加,我不相信,因爲它不是一個合適的init函數?我不知道爲什麼。

但我不在乎,因爲下面的修正版修復了漏洞,事實證明,Apple推薦使用的是什麼(我認爲我使用的是來自示例項目的舊代碼)。

這裏,似乎有固定的泄漏,在新版本的修訂「newBlankFormat:thisNewFormat」功能:

- (Format *)newBlankFormat:(Format *)thisNewFormat { 
      NSLog(@"making new blank format"); 
      gfghfAppDelegate *del = (gfghfAppDelegate *)[UIApplication sharedApplication].delegate; 
      NSManagedObjectContext *MOC = del.managedObjectContext; 
      //[MOC reset]; don't do this, it will erase MANAGED_OBJECT_2 
      thisNewFormat = [NSEntityDescription insertNewObjectForEntityForName:@"Format" inManagedObjectContext:MOC]; //this is much better than using the malloc variety 
      thisNewFormat.slot = [NSNumber numberWithInt:-1]; 
      NSLog(@"slot = %@",thisNewFormat.slot); 
      return thisNewFormat; 
} 
+0

這實際上不起作用,因爲[MOC重置]將MANAGED_OBJECT_2清除出上下文。編輯它以顯示如何修復。 – CommaToast

+0

另請注意:自動釋放的對象會造成我的應用程序崩潰,因爲運行循環會在另一個對象可以在傳遞的消息中接收到它之前結束。必須回到第一個問題中的原始方法,並在它指向新分配的Format對象以清除泄漏之前在「thisNewFormat」中添加一個版本,同時還創建一個將在傳遞時保留的對象圍繞着各種對象和方法。 – CommaToast