self.string 不保留导致内存泄漏 iPhone

发布于 2024-11-17 20:35:59 字数 346 浏览 2 评论 0原文

我有一些被声明为非原子并保留的日期和字符串。

当我使用 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 技术交流群。

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

发布评论

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

评论(4

诗化ㄋ丶相逢 2024-11-24 20:35:59

您要么释放该字符串两次,要么在释放该字符串后访问它。如果您发布一些代码,如果这还不够,它将有助于确定发生了什么。

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.

享受孤独 2024-11-24 20:35:59

考虑sequence:

0 NSObject *obj = [[NSObject alloc] init];
1 [obj retain]
2 [obj release]
3 [obj release]
4 NSLog("@%@",obj);
5 [obj retain]

将在第4行崩溃(obj已被释放),而sequence:

0 NSObject *obj = [[NSObject alloc] init];
1 [obj retain]
2 [obj retain]
3 [obj release]
4 [obj release]
4 NSLog("@%@",obj);

将泄漏(obj保留计数为1)。

正如您所看到的,相同的操作但顺序不同可以将您的对象从额外释放更改为泄漏。

Consider the sequence:

0 NSObject *obj = [[NSObject alloc] init];
1 [obj retain]
2 [obj release]
3 [obj release]
4 NSLog("@%@",obj);
5 [obj retain]

will crash at line 4 (obj has been deallocated), while the sequence:

0 NSObject *obj = [[NSObject alloc] init];
1 [obj retain]
2 [obj retain]
3 [obj release]
4 [obj release]
4 NSLog("@%@",obj);

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.

守护在此方 2024-11-24 20:35:59

你综合了 setter 和 getter 方法了吗?它们是通过在 .m 文件中包含以下代码行来合成的:

@synthesize time;

通常,您可以在以下行之后执行此操作:

@implementation YourClass

这实际上是在您设置变量时生成保留变量的方法,就像您在以下位置中所做的那样:

self.time = [cal dateFromComponents:comps];

Have you synthesized the setters and getter methods? They are synthesized by including the following line of code in the .m file:

@synthesize time;

Generally, you would do this right after the line:

@implementation YourClass

That's what actually generates the method that retains the variable when you set it, as you do, in:

self.time = [cal dateFromComponents:comps];
涙—继续流 2024-11-24 20:35:59

现在最好的学习是如何使用 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.

enter image description here

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