NSMutableString 在追加或替换OccurrencesOfString 时泄漏

发布于 2024-09-03 06:02:37 字数 1805 浏览 6 评论 0原文

我知道类似的问题已经被一次又一次地问到,但我请求您耐心等待,因为我似乎找不到有帮助的答案。

我的应用程序存在漏洞,这让我抓狂。实际上,它们并没有使用 Leaks 报告为泄漏,但是 ObjectAlloc 中的净字节不断增加,并且永远不会停止,如果持续时间足够长(不是很长),最终会导致崩溃。

NSMutableStrings 会出现此问题。我认为要么有一些我不理解的基本问题,要么我面临着另一个问题,我很难追踪但一直隐藏在 NSMutableStrings 后面。具体来说,我注意到每当我追加到 NSMutableString 或对 NSMutableString 执行替换时,ObjectAlloc 都会在调整 NSMutableString 大小时报告幕后 malloc/free 中似乎不匹配的内容。我很遗憾地说,这是我第二次遇到这个问题 - 第一次我花了几个小时,最后问题消失了(神奇!),但我真的不知道为什么。

当我查看下面的代码时(相信我,我已经盯着它看了几个小时),我看不到问题。我查看代码,心想我应该没问题,因为我正在释放我负责的唯一对象(aString),并且 NSMutableString 应该在调整大小后负责清理工作。在第二个示例中,为了以防万一它有帮助,传入的字符串来自 ASIHTTPRequest 对象(它是responseString),并且我根本不对其执行任何操作。它被简单地像这样调用 ([self DoStuff2:[request responseString]]) 并且我自己也不释放请求(我正在使用 ASINetworkQueue 并且我假设请求已为我销毁(我尝试并导致了错误)因为请求已经在其他地方释放了)。

另外,我知道它不应该做任何事情,但我什至尝试将代码包装在自动释放池中,这当然没有任何作用,我应该提到该代码是在其中运行的。我想也许我遇到了问题,因为 NSOperations 应该为自己创建一个自动释放池,但我已经尝试过,

与 NSMutableString 无关,但我发现使用 NSString ComponentsSeparatedByString 方法也有类似的问题。有时,获取分离组件的数组所使用的内存永远不会被释放......一般来说,字符串对我来说似乎有些问题,

如果您需要更多信息,我将不胜感激。我会很高兴添加它。我向你保证,我已经在这个问题(以及其他问题)上挣扎了好几个星期,我遇到的每个问题我都经过长时间的努力研究,直到找到解决方案 - 这不是一个无聊的请求,而是真正的求助!我已经编写了很多代码,现在我正在尝试密封一些小泄漏等,我注意到这个问题。老实说,我不敢相信 Objective C 中的内存管理有时会如此难倒我......我已经多次阅读 Apple 的内存管理文档,我认为我完全理解它,并且我努力努力释放我拥有的对象,但是有时我发现自己想知道我是否真正理解......我想把它放在床上一次并确保我完全理解这一切 - 在编写数千行代码后遇到这种问题/问题不仅仅是一点可怕/尴尬/烦人。

再说一遍,如果有人有任何见解,我将不胜感激。感谢您的时间和努力。

-(void)DoStuff
{
NSString *aString [ [[NSString alloc] initWithFormat:@"text %@ more text", self.strVariable];
[self.someMutableStringVar replaceOccurrencesOfString:@"replace" withString:aString options:NSCaseInsensitiveSearch range:NSMakeRange(0, [self.someMutableStringVar length])];
[aString release];
}


-(void)DoStuff2:(NSString *)aString
{
[self.someMutableStringVar appendString:aString];
}

I know similar questions have been asked time and time again but I ask that you please bear with me as I cannot seem to find an answer that helps.

My application has leaks that are driving me out of my mind. Actually, they are not reported as leaks using Leaks, but my net bytes in ObjectAlloc goes up and up and up and never stops, eventually leading to a crash if it goes on long enough (not very long).

The problem occurs with NSMutableStrings. I think there is either something fundamental I don't understand about them, or I am facing another problem that I am having difficulty tracking down but keeps hiding behind the NSMutableStrings. Specifically, I am noticing that whenever I append to or perform a replace on a NSMutableString, ObjectAlloc reports what appear to be mismatches in malloc/frees behind the scene when resizing the NSMutableString. I'm sorry to say this is the second time I'm facing this problem - the first time I messed around for hours and hours and finally the problem went away (magic!) but I don't really know why.

When I look at the code below (and believe me, I've stared at it for hours) I cannot see the problem. I look at the code and think to myself that I should be fine because I'm releasing the only object for which I am responsible (aString) and that NSMutableString should be taking care of cleaning up after any resizing it does. In the second example, just so you know in case it helps, the string being passed in comes from an ASIHTTPRequest object (it's the responseString) and I don't do anything at all with it. It's being called simply like so ([self DoStuff2:[request responseString]]) and I don't free the request myself either (I'm using a ASINetworkQueue and I assume that the requests are destroyed for me (I tried and caused errors because the request was already being release somewhere else).

Also, I know it shouldn't do anything, but I even tried wrapping the code in autorelease pools, which of course did nothing. I should mention that this code is being run inside of an NSOperation. I thought that perhaps I am experiencing problems because NSOperations should create an autorelease pool for themselves, but I've tried that to no avail.

Not related to NSMutableString, but I find I also have similar problems using the NSString componentsSeparatedByString method. Sometimes the memory used by the array that gets the separated components is never released. Hmmm...strings in general seem to be somewhat problematic for me it seems.

I would appreciate ANY help anyone can provide. If you require more info, I'll be glad to add it. I do promise you that I've struggled with this (and other problems) for weeks and every problem I encounter I research hard and long until I find a solution - this is not an idle request, but a true cry for help! I've written so much code and now I'm trying to seal some small leaks etc and I notice this problem. Honestly, I cannot believe how memory management in Objective C can stump me so at times...I've read Apple's memory mgmt docs many times and I thought I thoroughly understood it and I try to be diligent about releasing objects I own, but sometimes I find myself wondering if I truly understand...I would like to put this to bed once and make sure I understand all this fully - to have this sort of question/problem after writing thousands of lines of code is more than a little scary/embarrassing/annoying.

So again, if anybody has any insight, I'd be grateful. Thanks for your time and efforts.

-(void)DoStuff
{
NSString *aString [ [[NSString alloc] initWithFormat:@"text %@ more text", self.strVariable];
[self.someMutableStringVar replaceOccurrencesOfString:@"replace" withString:aString options:NSCaseInsensitiveSearch range:NSMakeRange(0, [self.someMutableStringVar length])];
[aString release];
}


-(void)DoStuff2:(NSString *)aString
{
[self.someMutableStringVar appendString:aString];
}

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

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

发布评论

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

评论(2

貪欢 2024-09-10 06:02:37

您发布的代码正确遵循 Cocoa 内存使用指南。不过,您报告的症状是内存增长,而不是泄漏。您如何确定这些是发生生长的位置?据推测,Instruments 中的 ObjectAlloc 工具正在显示大量仍然存在的对象分配的这些位置?

如果是这种情况,我猜您可能缺少自动释放池。您显示的代码有许多可能导致对象自动释放的操作。如果发生这些操作时没有活动的自动释放池,则该内存将无法正确释放。如果您在未设置自动释放池的后台线程中执行这些操作,则可能会发生这种情况。

The code you've posted adheres correctly to the Cocoa memory usage guidelines. The symptom you're reporting, though, is memory growth, not a leak. How you know for sure that these are the locations at which the growth is occurring? Presumably the ObjectAlloc instrument in Instruments is showing these locations for a large number of still-living object allocations?

If that's the case, I would guess that you may be missing an autorelease pool. The code you've shown has a number of operations that may result in objects being autoreleased. If there is no active autorelease pool when these operations occur, that memory won't be released properly. This may happen if you're doing these operations in a background thread in which no autorelease pool has been set up.

我一直都在从未离去 2024-09-10 06:02:37

我建议的第一件事是进行构建和构建。分析。这通常可以帮助我找出明显错误的地方。

The first thing I would suggest is to do a Build & Analyze. That usually helps me figure out of something's obviously wrong.

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