addObject for NSMutableArray crashes!

假装没事ソ 提交于 2020-01-16 12:34:03

问题


I am dealing with a problem which is driving me mad. I have looked at other forums and my code seems to be ok, however it fails when invoking addObject method for NSMutableArray. I am implementing a simple xml parser class and storing some of the information in a NSMutableArray:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
      if([elementName isEqualToString:@"Image"]){
          [images setObject: [[imageFile copy] autorelease]  forKey: [NSNumber numberWithInt: imageId]];
          return;
      }

      if([elementName isEqualToString:@"Annotation"]){
          //Here create the annotation object
          Annotation *newAnnot = [[Annotation alloc] init];
          newAnnot.imageId = imageId;
          newAnnot.annDescription = [[annDescription copy] autorelease];
          newAnnot.annLocation = [[annLocation copy] autorelease];
          [annotations addObject: newAnnot];
          [newAnnot release];
          return;
      }

    if([elementName isEqualToString: @"Patient"] || [elementName isEqualToString: @"Images"] || [elementName isEqualToString: @"Annotations"]){
        [currentSection setString: @""]; 
    }else {
        [currentElement setString: @""];
    }


}

[annotations addObject: newAnnot] is making the application to receive a SIGABRT signal!

2010-11-08 17:15:00.786 Touches[1430:207] *** -[NSCFDictionary addObject:]: unrecognized selector sent to instance 0x4b1bb50 2010-11-08 17:15:00.788 Touches[1430:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary addObject:]: unrecognized selector sent to instance 0x4b1bb50' 2010-11-08 17:15:00.789 Touches[1430:207] Stack: (
    42174544,
    43332396,
    42183259,
    41645686,
    41642482,
    18858,
    39384333,
    70873863,
    70897681,
    39381417,
    17100,
    8863,
    2234926,
    38538931,
    38537114,
    2234111,
    38538931,
    38544407,
    38545466,
    38538931,
    38537114,
    2230794,
    2239193,
    103026,
    108372,
    134462,
    115959,
    147928,
    44216700,
    41453724,
    41449640,
    107041,
    140146,
    8296 ) terminate called after throwing an instance of 'NSException' Program received signal:  “SIGABRT”. (gdb) continue Program received signal:  “SIGABRT”.

Here a brief description of the code:

Annotation is a simple class derived from NSObject. I am implementing init and dealloc methods. The first one does nothing, just returns "self" whereas dealloc invokes parent and releases the 2 strings

@interface Annotation : NSObject {
    int imageId;
    NSString *annDescription;
    NSString *annLocation;
} 

@property (nonatomic, assign) int imageId;
@property (nonatomic, retain) NSString *annDescription;
@property (nonatomic, retain) NSString *annLocation;

Nothing strange until here. Then have a look at the class implementing the parsing:

@interface PatientBundle : NSObject <NSXMLParserDelegate> {
    //............
    NSMutableArray *annotations;
    //.............
}
@property (nonatomic, retain) NSMutableArray *annotations;

My init method looks like this:

- (id) initFromFile: (NSString*)pathToFile{
    //.....
    annotations = [[NSMutableArray alloc] initWithCapacity: 10];
    [addressParser setDelegate:self];
    //......
}

Debugging suggests the object I am adding into the "annotations" array is not nil, however I am unable to even insert the first one. Why do I get a NSInvalidArgumentException? Why NSCFDictionary :addObject if my array is of class NSMutableArray? Is anything about the use of NSMutableArray that I am missing?

My platform details: - Running iphone Simulator Version 4.1 (225) - Base SDK and deployment target: iOSDevice 4.1 (target IPAD) - GCC 4.2

Any clue will be kindly appreciated. Thanks in advance.

Luis


回答1:


Unrecognized selector sent to <blah> almost always means that the object in question was released, and another object (of a different type) was later allocated using the same storage.

To track this down, look into setting NSZombieEnabled, which keeps all freed objects around as "zombies" instead of actually freeing the memory.




回答2:


The error shows that the object you're sending addObject: to is an NSDictionary. (Remember that the actual class of an object can differ from how you've declared it, even if things are working.) This could happen if you have a stale pointer -- try NSZombieEnabled to track that one.

As a callback, it's even possible that your PatientBundle is no longer valid.




回答3:


John's comment is worth looking into. Do a project-wide find for all occurrences of "annotations". Also, use the debugger to ensure that the line you think it's stopping on is really the right one.

Also: I have had weird behavior in my apps when I refer to a property P as plain P instead of self.P. I think you should go through your code and make this change.

Just saw David's comment, which I think is connected with my comment. Plain P does not do the retain; self.P does. Fix this and your troubles may be over.



来源:https://stackoverflow.com/questions/4126047/addobject-for-nsmutablearray-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!