1

在访问关系中的实体时,我有一个奇怪的(对我而言)内存泄漏。

Series 和 Tiles 之间存在反比关系。

// set up the fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Series" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// grab all of the series in the core data store
NSError *error = nil;
availableSeries = [[NSArray alloc] initWithArray:[managedObjectContext executeFetchRequest:fetchRequest error:&error]];
[fetchRequest release];

// grab one of the series
Series *currentSeries = [availableSeries objectAtIndex:1];
// load all of the tiles attached to the series through the relationship    
NSArray *myTiles = [currentSeries.tile allObjects];  // 16 byte leak here!

Instruments 报告最后一行有一个由 NSPlaceHolderString 引起的 16 字节泄漏。

堆栈跟踪:

 2 UIKit UIApplicationMain
 3 UIKit -[UIApplication _run]
 4 CoreFoundation CFRunLoopRunInMode
 5 CoreFoundation CFRunLoopRunSpecific
 6 GraphicsServices PurpleEventCallback
 7 UIKit _UIApplicationHandleEvent
 8 UIKit -[UIApplication sendEvent:]
 9 UIKit -[UIApplication handleEvent:withNewEvent:]
10 UIKit -[UIApplication _runWithURL:sourceBundleID:]
11 UIKit -[UIApplication _performInitializationWithURL:sourceBundleID:]
12 Memory -[AppDelegate_Phone application:didFinishLaunchingWithOptions:] /Users/cfish/svnrepo/Memory/src/Memory/iPhone/AppDelegate_Phone.m:49
13 UIKit -[UIViewController view]
14 Memory -[HomeScreenController_Phone viewDidLoad] /Users/cfish/svnrepo/Memory/src/Memory/iPhone/HomeScreenController_Phone.m:58
15 CoreData -[_NSFaultingMutableSet allObjects]
16 CoreData -[_NSFaultingMutableSet willRead]
17 CoreData -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:]
18 CoreData -[NSSQLCore retainedRelationshipDataWithSourceID:forRelationship:withContext:]
19 CoreData -[NSSQLCore newFetchedPKsForSourceID:andRelationship:]
20 CoreData -[NSSQLCore rawSQLTextForToManyFaultStatement:stripBindVariables:swapEKPK:]
21 Foundation +[NSString stringWithFormat:]
22 Foundation -[NSPlaceholderString initWithFormat:locale:arguments:]
23 CoreFoundation _CFStringCreateWithFormatAndArgumentsAux
24 CoreFoundation _CFStringAppendFormatAndArgumentsAux
25 Foundation _NSDescriptionWithLocaleFunc
26 CoreFoundation -[NSObject respondsToSelector:]
27 libobjc.A.dylib class_respondsToSelector
28 libobjc.A.dylib lookUpMethod
29 libobjc.A.dylib _cache_addForwardEntry
30 libobjc.A.dylib _malloc_internal

我想我遗漏了一些明显的东西,但我不太清楚是什么。

谢谢你的帮助!

更新:我已将有问题的代码块复制到 applicationDidFinishLaunching 的第一部分,但它仍然泄漏。我的模型可能有问题吗?

4

2 回答 2

1

NSPlaceHolder 是基础库使用的“单例”*,用于在以下结构中延迟分配 NSSrting 的内存:

[[NSString alloc] initWithString:@"only know the size of this string in the init, not the alloc"];

如果您只泄漏一个*,那并不是真正的泄漏。

  • 我说“单例”和“一个”,但可以有多个 NSPlaceHolderStrings - 如果使用 allocWithZone,则每个 NSZone 一个。
于 2010-05-19T05:29:17.490 回答
0

你释放availableSeries吗?除此之外,我什么也没看到。

于 2010-05-18T15:52:36.980 回答