初始化期间存储的值永远不会读取 [英] Value stored during its initialization is never read

查看:71
本文介绍了初始化期间存储的值永远不会读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个游戏,以便可以更改其数据并将其保存回去.我在注释行上收到两个错误.为什么会出现这些错误.我分配了游戏,所以我必须正确发布它.这是我保存游戏的代码

I am trying to create a Game so that I can change its data and save it back. I get two errors that are on the commented lines. Why am I getting these errors. I allocated the Game so I should have to release it correct. Here is my code to save my Game

Game *newGame = [[Game alloc] init];//error 1
newGame = [gamesArray objectAtIndex:gameNumber];
[newGame setTheShotArray:shotArray];
[gamesArray replaceObjectAtIndex:gameNumber withObject:newGame];
NSString *path = [self findGamesPath];
[NSKeyedArchiver archiveRootObject:gamesArray toFile:path];
[newGame release];//error 2

我收到错误1,该错误表示从未读取存储在'newGame'中的值.

I get error 1 which says Value stored to 'newGame' during its initialization is never read.

第二个错误是调用者此时尚未拥有的对象的引用计数的错误减少.

The second error says Incorrect decrement of the reference count of an object that is not owned at this point by the caller.

这是什么意思?而且请不要告诉我,您需要阅读有关内存管理的知识,然后给我一个链接.请告诉我如何解决该问题.

What does this mean? And please don't tell me, you need to read up on memory management and just give me a link. Tell me how to fix the problem please.

推荐答案

Game *newGame = [[Game alloc] init];//error 1

创建新实例并拥有它,因为您已经使用过+alloc.

You create a new instance and you own it since you’ve used +alloc.

newGame = [gamesArray objectAtIndex:gameNumber];

您从gamesArray获取另一个实例,并将其分配给上一行中使用的相同变量.这意味着您已经丢失了对先前对象的引用,并且由于您拥有先前的对象,因此您有责任释放它.您没有,所以您正在泄漏该物体.

You obtain another instance from gamesArray and assign it to the same variable that was used in the previous line. This means that you’ve lost the reference to the previous object and, since you own the previous object, you’re responsible for releasing it. You don’t, so you’re leaking that object.

[newGame release];//error 2

此时,newGame通过gamesArray指向实例.您尚未拥有它,因为您尚未通过NARC获得它,因此不应释放它.

At this point newGame points to the instance via from gamesArray. You do not own it since you haven’t obtained it via NARC, hence you should not release it.

NARC:其名称包含newalloccopyretain的方法.

NARC: a method whose name contains new, alloc, copy, or is retain.

底线:您正在泄漏通过+alloc创建的对象,并且试图释放不属于您的对象.

Bottom line: you’re leaking the object that you’ve created via +alloc and you’re trying to release an object that you do not own.

这篇关于初始化期间存储的值永远不会读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆