NSMutableArray 的 addObject 崩溃了!

发布于 2024-10-01 16:51:29 字数 3461 浏览 1 评论 0原文

我正在处理一个让我发疯的问题。我查看了其他论坛,我的代码似乎没问题,但是在调用 NSMutableArray 的 addObject 方法时失败。我正在实现一个简单的 xml 解析器类,并将一些信息存储在 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] 正在使应用程序接收 SIGABRT 信号!

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”.

这里简单描述一下代码:

Annotation 是一个从 NSObject 派生的简单类。我正在实现 init 和 dealloc 方法。第一个不执行任何操作,只返回“self”,而 dealloc 调用父级并释放 2 个字符串,

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

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

直到这里为止没有什么奇怪的。然后看一下实现解析的类:

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

我的 init 方法如下所示:

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

调试表明我添加到“注释”数组中的对象不为零,但是我什至无法插入第一个对象。为什么我会收到 NSInvalidArgumentException?如果我的数组属于 NSMutableArray 类,为什么要使用 NSCFDictionary:addObject ?我是否缺少有关 NSMutableArray 的使用的任何信息?

我的平台详细信息: - 运行 iphone 模拟器版本 4.1 (225) - 基础 SDK 和部署目标:iOSDevice 4.1(目标 IPAD) - GCC 4.2

如有任何线索,我们将不胜感激。提前致谢。

路易斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面犯桃花 2024-10-08 16:51:29

发送到的无法识别的选择器几乎总是意味着有问题的对象已被释放,并且稍后使用相同的存储分配了另一个对象(不同类型)。

要追踪此问题,请查看设置 NSZombieEnabled,其中将所有释放的对象保留为“僵尸”,而不是实际释放内存。

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.

灯下孤影 2024-10-08 16:51:29

该错误表明您发送 addObject: 的对象是 NSDictionary。 (请记住,即使一切正常,对象的实际类也可能与您声明它的方式不同。)如果您有一个过时的指针,则可能会发生这种情况 - 尝试使用 NSZombieEnabled 来跟踪该指针。

作为回调,您的 PatientBundle 甚至可能不再有效。

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.

我乃一代侩神 2024-10-08 16:51:29

约翰的评论值得研究。对所有出现的“注释”进行项目范围的查找。另外,使用调试器来确保您认为它停止的行确实是正确的。

另外:当我将属性 P 称为普通 P 而不是 self.P 时,我在应用程序中出现了奇怪的行为。我认为您应该检查您的代码并进行此更改。

刚刚看到大卫的评论,我认为这与我的评论有关。 Plain P 不做retain; self.P 确实如此。解决这个问题,你的麻烦可能就会结束。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文