Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

iphone - addObject for NSMutableArray crashes!

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...