arrayWithContentsOfFile:导致崩溃

时间:2010-10-09 21:40:35

标签: objective-c cocoa memory nszombie

我正在创建一个应用程序,我收到一个EXC_BAD_ACCESS错误。


CODE

@interface DNProjectsCategory : DNCategory {
  NSArray *projects;
}

@property(nonatomic, retain) NSArray *projects;

@end

@implementation DNProjectsCategory
@synthesize projects;

// MEM

- (void)dealloc {
  [projects release];

  [super dealloc];
}

// INIT.
- (id)init {
  if (self = [super init]) {
    title = NSLocalizedString(@"PROJECTS", nil);
    isSubCategory = NO;

    // Initialize projects
    //!!LINE 32 IS HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    projects = [NSArray arrayWithContentsOfFile:DNPROJECTSFILE];
  }

  return self;
}

// CATEGORIES

- (NSArray *)subCategories {
  NSMutableArray *projectsArray = [[[NSMutableArray alloc] init] autorelease];

  for (NSDictionary *project in projects) {
    DNCategory *projectCategory = [[DNCategory alloc] initWithTitle:[project valueForKey:@"title"]
                                                      subCategories:nil
                                                      isSubCategory:YES];

    [projectsArray addObject:projectCategory];
    [projectCategory release];
  }

  return projectsArray;
}

DNPROJECTSFILE的内容

请参阅http://gist.github.com/618628


CONSOLE& INSTRUMENTS

这是控制台在运行时所说的内容(NSZombie已启用):

run
[Switching to process 41257]
Running…
2010-10-09 23:32:36.899 Done[41257:a0f] *** -[CFString isKindOfClass:]: message sent to deallocated instance 0x1001caab0
sharedlibrary apply-load-rules all

以下是仪器在NSZombie测试中所说的内容:

  

Zombie Messaged

     

将Objective-C消息发送到地址为0x10012af80的解除分配对象(僵尸)。

  

堆栈跟踪

   0 CoreFoundation _CFRuntimeCreateInstance
   1 CoreFoundation __CFStringCreateImmutableFunnel3
   2 CoreFoundation CFStringCreateWithBytes
   3 CoreFoundation _uniqueStringForCharacters
   4 CoreFoundation getString
   5 CoreFoundation parseXMLElement
   6 CoreFoundation parseXMLElement
   7 CoreFoundation parseArrayTag
   8 CoreFoundation parseXMLElement
   9 CoreFoundation parsePListTag
  10 CoreFoundation parseXMLElement
  11 CoreFoundation _CFPropertyListCreateFromXMLStringError
  12 CoreFoundation _CFPropertyListCreateWithData
  13 CoreFoundation CFPropertyListCreateFromXMLData
  14 Foundation _NSParseObjectFromASCIIPropertyListOrSerialization
  15 Foundation +[NSArray(NSArray) newWithContentsOf:immutable:]
  16 Foundation +[NSArray(NSArray) arrayWithContentsOfFile:]
  17 Done -[DNProjectsCategory init] /Users/rsonic/Developer/Done/DNProjectsCategory.m:32
  18 Done -[DNBindingsController categories] /Users/rsonic/Developer/Done/DNBindingsController.m:18
  19 Foundation -[NSObject(NSKeyValueCoding) valueForKey:]
  20 Foundation -[NSObject(NSKeyValueCoding) valueForKeyPath:]
  21 AppKit -[NSBinder valueForBinding:resolveMarkersToPlaceholders:]
  22 AppKit -[NSArrayDetailBinder _refreshDetailContentInBackground:]
  23 AppKit -[NSObject(NSKeyValueBindingCreation) bind:toObject:withKeyPath:options:]
  24 AppKit -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:]
  25 AppKit loadNib
  26 AppKit +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]
  27 AppKit +[NSBundle(NSNibLoading) loadNibNamed:owner:]
  28 AppKit NSApplicationMain
  29 Done main /Users/rsonic/Developer/Done/main.m:13
  30 Done start

问题

我真的不知道如何修复这个双版本。据我所知,我不会在projects以外的任何地方发布dealloc变量。请问有人可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:6)

您没有使用访问者,因此永远不会保留项目。 init方法有两种选择:

projects = [[NSArray arrayWithContentsOfFile:DNPROJECTSFILE] retain];

self.projects = [NSArray arrayWithContentsOfFile:DNPROJECTSFILE];

@property(nonatomic, retain) NSArray *projects;

属性“创建”两个方法,一个getter - (NSArray *)projects和一个,在你的情况下更重要的是,setter - (void)setProjects:(NSArray*)a;你在属性声明中写的retain语句只适用于setter。如果直接使用projects = foo;设置变量,则不使用setter 但self.projects = foo;相当于[self setProject:foo],这是您动态创建的制定者 你的二传手看起来与此相似:

- (void)setProjects:(NSArray*)anArray {
    [anArray retain];
    [projects release];
    projects = anArray;
}

因此,如果您使用setter,则会保留您从arrayWithContentsOfFile获得的自动释放的NSArray。 您在Objective C中进行的每次调用都不是“alloc”,“copy”,“retain”或以new开头的任何内容返回一个自动释放的对象。如果您想稍后使用它们(即在您离开创建它们的方法之后),则必须保留它们。

也许您想再看看Apple Memory Managment Guide