NSNumber numberWithInt 在数字 >= 13 上崩溃

发布于 2024-11-30 22:34:30 字数 1873 浏览 2 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

懵少女 2024-12-07 22:34:30

尝试用一行代码包围您的代码(您已将其放置在 main 中)以创建然后耗尽自动释放池:

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];

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];
苏大泽ㄣ 2024-12-07 22:34:30

请尝试以下操作:

  1. 分配时释放array1array2

    [数组1释放];
    [array2 release];

  2. 创建并释放自动释放池:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    ...

    [pool release];

Try the following:

  1. Release array1 and array2 as you alloced these:

    [array1 release];
    [array2 release];

  2. Create and release an autoreleasepool:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    ...

    [pool release];

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