NSNumber numberWithInt崩溃数字> = 13 [英] NSNumber numberWithInt crashing on numbers >= 13
问题描述
基本上,我这样做:
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.\\\
);
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.\\\
);
return 0;
}
打印1.然后崩溃,如上所述。这是我的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)
$(主):$(OBJECTS)
$(CXX)-o $ @ $ ^ $(LINK_FLAGS)
% :%.m $(HEADERS)
$(CXX)-c $< $(COMP_FLAGS)
清洁:
$(RM)$(MAIN)$(OBJECTS)
尝试围绕你的代码(你已经放在主),用一行来创建一个自动释放池,然后消耗一个自动释放池:
NSAutoReleasePool * pool = [[NSAutoReleasePool alloc] init];
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];
[pool drain];
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 $< $(COMP_FLAGS)
clean:
$(RM) $(MAIN) $(OBJECTS)
Try surrounding your code (which you've placed in main) with a line to create and then drain an auto release pool:
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
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];
[pool drain];
这篇关于NSNumber numberWithInt崩溃数字> = 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!