返回 NSObject - 执行错误

发布于 2024-10-15 07:56:50 字数 1538 浏览 6 评论 0原文

我一直在努力解决一个问题,我希望有人可以帮助我。

我有一个名为“GameObjectDefinitionTable”的类,我在其中设置了所有对象属性,该属性位于另一个名为“Product”的类中。在我的“HelloWorldScene”中,我分配了“GameObjectDefinitionTable”,它又创建了多个“产品”。像这样:

HelloWorldScene ->游戏对象定义表 ->产品

然后我想将“产品”返回到“HelloWorldScene”。但这就是我遇到问题的地方。一些代码:

HelloWorldScene:

GameObjectDefinitionTable *god = [[GameObjectDefinitionTable alloc]init];
Product* currentProduct = [god getProductWithNum:0];
NSLog(@"currenProduct (name): %@",currentProduct.name); //Crash

GameObjectDefinitionTable:

-(void) createProducts {
    Product *product;

    for (int i=0; i<[allProductsWithDefinitions count];i++ ) {  
        product = [[Product alloc]init];
        product.name = [[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:0];
        product.density = [[[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:1] floatValue];
        product.scoreValue = [[[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:2] intValue];
        product.fileName = [[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:3];
        [products addObject:product];   
        [product release];
    }

    [allProductsWithDefinitions release];
}

-(Product *) getProductWithNum:(int)tNum {
    Product *tempProduct;
    tempProduct = [products objectAtIndex:tNum];

    return tempProduct;
    [tempProduct release]; 
}

如果我登录该类,“GameObjectDefinitionTable”中的数组和所有内容都工作正常。

非常感谢您的回答:)

I've been struggling with a problem that I hope someone can help me with.

I have Class called 'GameObjectDefinitionTable', where I set all my object properties, which is in another class called 'Product'. In my 'HelloWorldScene' I allocate 'GameObjectDefinitionTable' which in turn creates several 'Product's. Like this:

HelloWorldScene -> GameObjectDefinitionTable -> Product

I then want to return a 'Product' to 'HelloWorldScene. But here is where I get problems. Some code:

HelloWorldScene:

GameObjectDefinitionTable *god = [[GameObjectDefinitionTable alloc]init];
Product* currentProduct = [god getProductWithNum:0];
NSLog(@"currenProduct (name): %@",currentProduct.name); //Crash

GameObjectDefinitionTable:

-(void) createProducts {
    Product *product;

    for (int i=0; i<[allProductsWithDefinitions count];i++ ) {  
        product = [[Product alloc]init];
        product.name = [[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:0];
        product.density = [[[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:1] floatValue];
        product.scoreValue = [[[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:2] intValue];
        product.fileName = [[allProductsWithDefinitions objectAtIndex:i] objectAtIndex:3];
        [products addObject:product];   
        [product release];
    }

    [allProductsWithDefinitions release];
}

-(Product *) getProductWithNum:(int)tNum {
    Product *tempProduct;
    tempProduct = [products objectAtIndex:tNum];

    return tempProduct;
    [tempProduct release]; 
}

The arrays and all in 'GameObjectDefinitionTable' is working fine if I log in that class.

Would be really grateful for an answer :)

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

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

发布评论

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

评论(2

把梦留给海 2024-10-22 07:56:50

您是否需要类似的情况:

- (Product *)getProductWithNum:(int)tNum
{
  Product *tempProduct = [[products objectAtIndex:tNum] retain];
  return [tempProduct autorelease];
}

Is it the case you need something like:

- (Product *)getProductWithNum:(int)tNum
{
  Product *tempProduct = [[products objectAtIndex:tNum] retain];
  return [tempProduct autorelease];
}
深居我梦 2024-10-22 07:56:50

产品 *tempProduct;

tempProduct = [产品 objectAtIndex:tNum];

返回 tempProduct;

[临时产品发布];

是那是你想要拥有的吗?这里有两个相互抵消的大问题。 [tempProduct release]; 行无法访问。第二,如果您要在返回之前实际执行 [tempProduct release];,您将释放内存,然后访问当前产品的 currentProduct.name 属性本质上是一个悬空指针。这种非法内存访问可能会导致 Bad Exec。

由于您没有分配、复制或保留 tempProduct,因此不得释放它。为什么不只是简单的return [products objectAtIndex:tNum];

Product *tempProduct;

tempProduct = [products objectAtIndex:tNum];

return tempProduct;

[tempProduct release];

Is that what you mean to have? You kind of have two big problems cancelling each other out here. The [tempProduct release]; line is unreachable. The second, if you were to actually execute [tempProduct release]; before the return, you would be releasing the memory, and then accessing the currentProduct.name property of what is essentially a dangling pointer. This illegal memory access could cause your Bad Exec.

Since you are not allocating, copying, or retaining tempProduct, you must NOT release it. Why not just a simple return [products objectAtIndex:tNum];?

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