self.string 不保留导致内存泄漏 iPhone
我有一些被声明为非原子并保留的日期和字符串。
当我使用 self.string 设置日期或更改字符串时,它不会被保留。如果我通过 [string keep] 强制它,我不会有任何问题,尽管这会导致内存泄漏。
有什么想法或者我做错了什么吗?
干杯
更新:
@property (nonatomic, retain)NSDate *time;
self.time = [cal dateFromComponents:comps];
self.time
不保留。我必须[时间保留];
I have a few dates and strings that are declared as nonatomic and retained.
When I set the date or change the string using self.string
it isn't being retained. If I force it by [string retain]
I have no issues, although this creates memory leaks.
Any ideas or how or what I'm doing wrong?
Cheers
Update:
@property (nonatomic, retain)NSDate *time;
self.time = [cal dateFromComponents:comps];
self.time
doesn't retain. I have to [time retain];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您要么释放该字符串两次,要么在释放该字符串后访问它。如果您发布一些代码,如果这还不够,它将有助于确定发生了什么。
You are either releasing the string twice or accessing it after it has already been released. If you post some code, it will help determine what's going on if that's not enough to go by.
考虑sequence:
将在第4行崩溃(obj已被释放),而sequence:
将泄漏(obj保留计数为1)。
正如您所看到的,相同的操作但顺序不同可以将您的对象从额外释放更改为泄漏。
Consider the sequence:
will crash at line 4 (obj has been deallocated), while the sequence:
will leak (obj retain count is 1).
As you can see the same operations but in different order can change your object from being extra-deallocated to leaked.
你综合了 setter 和 getter 方法了吗?它们是通过在 .m 文件中包含以下代码行来合成的:
通常,您可以在以下行之后执行此操作:
这实际上是在您设置变量时生成保留变量的方法,就像您在以下位置中所做的那样:
Have you synthesized the setters and getter methods? They are synthesized by including the following line of code in the .m file:
Generally, you would do this right after the line:
That's what actually generates the method that retains the variable when you set it, as you do, in:
现在最好的学习是如何使用 Instruments 找到此类问题的原因。
在 Xcode 4 中,转到“产品/配置文件”,您的项目将被编译,并且将出现“仪器”窗口。选择分配工具,然后通过单击看起来像 REC 按钮的红色按钮来停止自动启动的执行。
单击分配工具前面的小 i 按钮。并选择“记录引用计数”和“启用 NSZombie 检测”,现在通过单击同一红色按钮再次运行。重现问题,当崩溃时返回“仪器”窗口,您将看到一个对话框,单击箭头并搜索问题的根源。 IMO 你应该看看你的对象被错误释放的地方。
The best thing to learn right now is how to find the cause of these kind of problems using Instruments.
In Xcode 4 go to Product / Profile, your project will compiled and Instruments window will appear. Choose the Allocations instrument, then stop automatically started execution by clicking on the red button that looks like a REC button.
Click on the little i button in front of Allocations instrument. And select "Record Reference Count" and "Enable NSZombie Detection", now run again by clicking that same red button. Reproduce the problem, when it crashs go back to the Instruments window and you will see a dialog box click on the arrow and search for the problem's origin. IMO you should look where your object is been wrongly released.