返回 autorelease NSString 仍然会导致内存泄漏

发布于 2024-08-26 11:22:14 字数 733 浏览 8 评论 0原文

我有一个简单的函数,在解码后返回一个 NSString 。我在我的应用程序中经常使用它,每次使用它时,它似乎都会造成内存泄漏(根据“泄漏”工具)。 Leaks 告诉我问题出在我分配要返回的 NSString 的行上,即使我自动释放它。这是函数:

-(NSString *) decodeValue
{
 NSString *newString;
 newString = [self stringByReplacingOccurrencesOfString:@"#" withString:@"$"];
 NSData *stateData = [NSData  dataWithBase64EncodedString:newString];
 NSString *convertState = [[[NSString alloc] initWithData:stateData encoding:NSUTF8StringEncoding] autorelease];
 return convertState;
}

我对 [autorelease] 的理解是,它应该以这种方式使用......我想保留该对象足够长的时间以在我的函数中返回它,然后让该对象稍后自动释放。所以我相信我可以通过这样的代码使用这个函数,而无需手动释放任何东西:

NSString *myDecodedString = [myString decodeValue];  

但是这个过程正在报告泄漏,我不明白如何更改它以避免泄漏。我做错了什么?

I have a simple function that returns an NSString after decoding it. I use it a lot throughout my application, and it appears to create a memory leak (according to "leaks" tool) every time I use it. Leaks tells me the problem is on the line where I alloc the NSString that I am going to return, even though I autorelease it. Here is the function:

-(NSString *) decodeValue
{
 NSString *newString;
 newString = [self stringByReplacingOccurrencesOfString:@"#" withString:@"$"];
 NSData *stateData = [NSData  dataWithBase64EncodedString:newString];
 NSString *convertState = [[[NSString alloc] initWithData:stateData encoding:NSUTF8StringEncoding] autorelease];
 return convertState;
}

My understanding of [autorelease] is that it should be used in exactly this way... where I want to hold onto the object just long enough to return it in my function and then let the object be autoreleased later. So I believe I can use this function through code like this without manually releasing anything:

NSString *myDecodedString = [myString decodeValue];  

But this process is reporting leaks and I don't understand how to change it to avoid the leaks. What am I doing wrong?

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

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

发布评论

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

评论(2

怪异←思 2024-09-02 11:22:14

我认为泄漏正在让你误入歧途。 Leaks 会告诉你被泄漏的对象最初分配在哪里,不一定与泄漏的原因相同。这段代码看起来不错 - 可能发生的情况是这个结果被保留在另一个类中的某个地方并且从未在那里发布。

I think leaks is leading you astray. Leaks will tell you where the object being leaked was originally allocated, not necessarily the same as the reason for the leak. This code seems fine - what's probably happening is that this result is being retained in another class somewhere and never released there.

最丧也最甜 2024-09-02 11:22:14

Jexe 是对的,Leaks 工具不可能知道泄漏在哪里,它只知道实例的创建点,并且在 RunLoop 结束时表示对象仍然被保留,这就是它指向 alloc 调用的原因。它仍然是一个很大的帮助,现在你只需要弄清楚还有谁可以访问 ConvertState 并保留它。

Jexe is right, the Leaks tool cannot possibly know where the leak is, all it knows is the point of creation of an instance and that at the end of the RunLoop said object still was being retained, that's why it's pointing to the alloc call. It's still a big help, now you just have to figure out who else is having access to convertState and retaining it.

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