返回 NSObject - 执行错误
我一直在努力解决一个问题,我希望有人可以帮助我。
我有一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否需要类似的情况:
Is it the case you need something like:
是那是你想要拥有的吗?这里有两个相互抵消的大问题。
[tempProduct release];
行无法访问。第二,如果您要在返回之前实际执行[tempProduct release];
,您将释放内存,然后访问当前产品的currentProduct.name
属性本质上是一个悬空指针。这种非法内存访问可能会导致 Bad Exec。由于您没有分配、复制或保留
tempProduct
,因此不得释放它。为什么不只是简单的return [products objectAtIndex:tNum];
?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 thecurrentProduct.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 simplereturn [products objectAtIndex:tNum];
?