NSNumber numberWithInt 在数字 >= 13 上崩溃
我对 Objective-C 还很陌生。我已阅读类似的问题,但我无法弄清楚了解如何利用该信息解决我的问题。
基本上,我正在这样做:
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n1 = [NSNumber numberWithInt: 12];
[array1 addObject: n1];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n2 = [NSNumber numberWithInt: 13];
[array2 addObject: n2];
将 NSNumber 12 添加到数组中工作得很好,但添加 13 (或任何更高的值)则不行;程序在运行时崩溃(没有错误消息,并且生成的 stackdump 文件完全是空白的)。我正在 Cygwin 中使用 gcc 进行编译,如果这很重要的话。 我知道这可能与保留计数有关,就像我上面提到的问题一样,但我不知道如何解决它。即使我注释掉最后一行,它也会崩溃......所以它在 numberWithInt 调用时崩溃,这意味着如果我为 n2 添加保留语句,它无论如何都不会被调用。
编辑:由于我被要求提供更多代码,这是我为了测试这个问题而制作的文件:
#import <stdio.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSValue.h>
int main( int argc, const char *argv[] )
{
printf("1.\n");
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n1 = [NSNumber numberWithInt: 12];
[array1 addObject: n1];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n2 = [NSNumber numberWithInt: 13];
[array2 addObject: n2];
printf("2.\n");
return 0;
}
这会打印“1”。然后崩溃,如上所述。这是我的生成文件:
CYGWIN_GNUSTEP_PATH=/cygdrive/c/GNUstep
CXX = gcc
MAIN = DummyGame
SOURCES = DummyGame.m
OBJECTS = $(SOURCES:%.m=%.o)
COMP_FLAGS = -std=c99 -I $(CYGWIN_GNUSTEP_PATH)/GNUstep/System/Library/Headers -L $(CYGWIN_GNUSTEP_PATH)/GNUstep/System/Library/Libraries -fconstant-string-class=NSConstantString
LINK_FLAGS = $(COMP_FLAGS) -lobjc -lgnustep-base
all: $(MAIN)
$(MAIN): $(OBJECTS)
$(CXX) -o $@ $^ $(LINK_FLAGS)
%.o: %.m $(HEADERS)
$(CXX) -c $< $(COMP_FLAGS)
clean:
$(RM) $(MAIN) $(OBJECTS)
I'm pretty new to Objective-C. I've read through a similar question but I can't figure out how to solve my problem with that information.
Basically, I'm doing this:
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n1 = [NSNumber numberWithInt: 12];
[array1 addObject: n1];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n2 = [NSNumber numberWithInt: 13];
[array2 addObject: n2];
Adding the NSNumber 12 to the array works perfectly fine, but adding 13 (or anything higher) does not; the program crashes at runtime (no error messages, and the stackdump file produced is completely blank). I'm compiling with gcc in Cygwin, if that matters.
I understand that this is probably related to retain counts, as in the question I mentioned above, but I don't know how to fix it. Even if I comment out the last line, it crashes... so it's crashing right at the numberWithInt call, meaning that if I add a retain statement for n2, it won't have a chance to get called anyway.
edit: Since I was asked for more code, here's the file I made in order to test this problem:
#import <stdio.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSValue.h>
int main( int argc, const char *argv[] )
{
printf("1.\n");
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n1 = [NSNumber numberWithInt: 12];
[array1 addObject: n1];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity: 1];
NSNumber *n2 = [NSNumber numberWithInt: 13];
[array2 addObject: n2];
printf("2.\n");
return 0;
}
This prints "1." and then crashes, as above. Here is my makefile:
CYGWIN_GNUSTEP_PATH=/cygdrive/c/GNUstep
CXX = gcc
MAIN = DummyGame
SOURCES = DummyGame.m
OBJECTS = $(SOURCES:%.m=%.o)
COMP_FLAGS = -std=c99 -I $(CYGWIN_GNUSTEP_PATH)/GNUstep/System/Library/Headers -L $(CYGWIN_GNUSTEP_PATH)/GNUstep/System/Library/Libraries -fconstant-string-class=NSConstantString
LINK_FLAGS = $(COMP_FLAGS) -lobjc -lgnustep-base
all: $(MAIN)
$(MAIN): $(OBJECTS)
$(CXX) -o $@ $^ $(LINK_FLAGS)
%.o: %.m $(HEADERS)
$(CXX) -c lt; $(COMP_FLAGS)
clean:
$(RM) $(MAIN) $(OBJECTS)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试用一行代码包围您的代码(您已将其放置在 main 中)以创建然后耗尽自动释放池:
Try surrounding your code (which you've placed in main) with a line to create and then drain an auto release pool:
请尝试以下操作:
在
分配
时释放array1
和array2
:[数组1释放];
[array2 release];
创建并释放自动释放池:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
...
[pool release];
Try the following:
Release
array1
andarray2
as youalloc
ed these:[array1 release];
[array2 release];
Create and release an autoreleasepool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
...
[pool release];