0

I think I have a problem, maybe linked to a retain cycle in Core-Data. The code is a follow, where self.image is also a NSManagedObject:

- (void)setImage:(UIImage*)image1 andThumbnail:(UIImage*)image2
{
    self.image.data = UIImageJPEGRepresentation(image1, 0.85); // This is autoreleased
    self.thumbnail = UIImageJPEGRepresentation(image2, 0.85); // This is autoreleased
}

Apparently, the "self.image.date =" has one retain that is never released (and I think that it is between self.image and self). Because of that the self object will never be released and hence the leak.

EDIT: so basically I have the same problem as here: https://devforums.apple.com/message/246219#246219 I use exactly the same structure where the self in the previous code corresponds to the Bar in the given link. I also have the same view controller structure. However, the refreshObject doesn't help.

I tried to use the NSManagedObjectContext refreshObject method to break the retain cycle (as suggested in Apple documentation). It has no influence on the retainCount. I'm probably not using it the right way but I can't find much information about it. If I use NSManagedObjectContext:reset: I get a crash in the root view controller when I come back to it.

Thanks!

4

1 に答える 1

2

管理オブジェクト コンテキストの管理オブジェクト メモリの管理に干渉しないでください。

上記self.imageがマネージド オブジェクトであり、カスタム アクセサーを作成していない場合、それによるメモリ管理の問題はありません。コンテキストのメモリを手動で管理しようとすると、ほとんどの場合、解決するよりも多くの問題が発生します。

最も単純で最小のコマンド ライン アプリ以外では、保持カウントは何の意味もありません。Core Data のようなフレームワークを使用すると、バックグラウンドでの保持が非常に複雑になるため、多くの場合、保持カウントは独自のコードで発生することとは関係ありません。

どうやら、「self.image.date =」には解放されない保持が1つあります(それはself.imageとselfの間にあると思います)。そのため、自己オブジェクトが解放されることはなく、リークが発生します。

これは起こりません。インスタンス自体を強制終了する前に、インスタンスの保持属性内のすべてのオブジェクトを強制終了する必要はありません。そうであれば、アトリビュート オブジェクトを 3 番目のオブジェクトと共有するインスタンスを削除できませんでした。それらが非 managedObject インスタンスである場合、オブジェクトは、self.imageオブジェクトが消滅した後も長く存在できますself。コンテキストによるエンティティ グラフの強制のみが、それらの動作を異なるものにし、それはメモリ管理とは何の関係もありません。

管理オブジェクトで不思議な保持カウント 1 が表示される場合、それは管理オブジェクト コンテキストによってオブジェクトに設定された保持です。管理対象オブジェクトがエンティティ グラフに存在する必要があるとコンテキストが判断している限り、オブジェクトは決して解放されません。

selfリークが Core Data スタックにある場合、エンティティとエンティティの間のエンティティ グラフに問題がある可能性が最も高くなりself.imageます。エンティティ グラフは、おそらく拒否または必要な関係によって、どちらか一方が削除されるのを妨げています。

于 2010-06-26T16:43:09.143 に答える