哪里漏了?

发布于 2024-10-06 12:04:29 字数 452 浏览 0 评论 0原文

Instruments 告诉我这段代码存在泄漏。不过,我很确定我后来发布了它。谁能告诉我这是怎么回事?

- (void) addReminderEntry{
    DataEntryController* item = [[DataEntryController alloc] initWithEntryType:REMINDER]; // it says that the leak was instantiated here
    item.delegate = self;
    [[self navigationController] pushViewController:item animated:YES];
    [item setEditing:YES animated:YES];
    [item release];// this is the place I release it
}

谢谢

The Instruments tells me that this piece of code has a leak. However, I am pretty sure I have released it later on. Can anyone tell me what's going on here?

- (void) addReminderEntry{
    DataEntryController* item = [[DataEntryController alloc] initWithEntryType:REMINDER]; // it says that the leak was instantiated here
    item.delegate = self;
    [[self navigationController] pushViewController:item animated:YES];
    [item setEditing:YES animated:YES];
    [item release];// this is the place I release it
}

Thanks

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

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

发布评论

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

评论(3

花开半夏魅人心 2024-10-13 12:04:29

它很可能与 DataEntryController 类中未发布的内容有关。确保您正在释放该类中的所有属性/等。

More than likely it has to do with something not being released within the DataEntryController class. Make sure you are releasing all your properties/etc within that class.

鹊巢 2024-10-13 12:04:29

泄漏仅告诉您内存分配的位置,它不能告诉您应该将释放内存的代码放在哪里!

这就是说你创建了一个视图控制器,并且在你完成它之后它仍然存在于内存中。是的,您在该代码中释放了 VC,但只有在您呈现它之后 - 这意味着导航控制器保留了它,并且可能还保留了其他内容。仅当调用最终版本时它才会被释放。

视图控制器未释放的主要原因通常是让视图控制器将自己设置为其保留的内容的委托,然后在视图控制器离开屏幕时不撤消该委托。如果您的视图控制器是保留它的事物的委托,那么它永远不会被释放。

Leaks tells you only where memory was allocated, what it cannot tell you is where to put the code that should have released it to start with!

So this is saying that you made a view controller, and it was still around in memory after you finished with it. Yes you release the VC in that code, but only after you present it - which means the navigation controller has retained it, and possibly other things. It only gets deallocated when the final release is called.

The main culprit for view controllers not being released is usually having the view controller set itself as a delegate for something it retains, and then not undoing that when the view controller goes offscreen. If your view controller is a delegate of something that retains it, it's never going to be deallocated.

2024-10-13 12:04:29

事实证明,这是由这个构造函数引起的:

- (DataEntryController*) initWithEntryType:(DataType) eType{
    DataEntryController* item = [[DataEntryController alloc] init];//<- here
    item.entryType = eType;
    item.allowEdit = YES;
    return item;
}

显然,iOS 为每个带有初始“init”的构造函数添加了retain 1。

切换到以下后工作正常:

DataEntryController* item = [super init];

It turns out that this is caused by this constructor:

- (DataEntryController*) initWithEntryType:(DataType) eType{
    DataEntryController* item = [[DataEntryController alloc] init];//<- here
    item.entryType = eType;
    item.allowEdit = YES;
    return item;
}

Apparently iOS adds retain 1 to each constructor with an initial 'init'.

It works fine after switching to:

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