内存泄漏问题 NSAutoreleaseNoPool()

发布于 2024-10-05 21:24:16 字数 573 浏览 8 评论 0原文

我正在尝试创建一个不可变的字符串。我没有用 init、alloc 或 new 初始化它,但内存仍然泄漏,它说“NSCFString 类的对象 0x234b533 自动释放,没有池 - 只是泄漏”,这就是我试图做的,

NSMutableString *srn = [NSMutableString stringwithCString:devSID];

// devSID is *char

这会导致泄漏。我也尝试过这个,

NSMutableString *srn = [NSMutableString stringwithCString:devSID length:sizeof(devSID)];

但这也不起作用,但是如果我尝试用像这样的简单字符串初始化它,

NSMutableString *srn = @"this is my string";

它会起作用,不知道周围发生了什么。我没有使用 init 或 alloc 但仍然存在泄漏。如果有人能帮助我解决这个问题,我将不胜

感激

Umair

I am trying to create an immutable string. I am not initializing it with init, alloc or new but still the memory is leaking and its saying "object 0x234b533 of Class NSCFString autoreleased with no pool in place - just leaking " here is what I am trying to do

NSMutableString *srn = [NSMutableString stringwithCString:devSID];

// devSID is *char

this leaves a leak. I have tried this too

NSMutableString *srn = [NSMutableString stringwithCString:devSID length:sizeof(devSID)];

but this too doesn't work, however if I try initialize it with a simple string like this

NSMutableString *srn = @"this is my string";

it works, dont have any idea whats happening around. I am not using init or alloc but still there is a leak. I would be obliged if anyone could help me out to resolve this issue

Regards

Umair

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

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

发布评论

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

评论(2

顾忌 2024-10-12 21:24:16

泄漏是由于您自动释放对象而没有适当的自动释放池来处理它而引起的。当您通过创建自己的线程或仅使用 -performSelectorInBackground:withObject: 便捷方法在主线程之外执行操作时,通常会发生这种情况。如果您想使用自动释放功能(通过使用此处的 NSMutableString 类方法来暗示),您需要在要使用它的代码块的开头创建一个自动释放池最后沥干。换句话说,是这样的:

- (void)myBackgroundThing:(id)whatever
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableString *srn = [NSMutableString stringwithCString:devSID];

    // etc...

    [pool release];
}

The leak is caused by your autoreleasing an object without having an autorelease pool in place to take care of it. That usually happens when you're doing things apart from the main thread, via creating your own threads or just using the -performSelectorInBackground:withObject: convenience method. If you want to use the autorelease functionality (implied by the use of the NSMutableString class method here), you need to create an autorelease pool at the beginning of the block of code where you'll be using it and drain it at the end. In other words, something along these lines:

- (void)myBackgroundThing:(id)whatever
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableString *srn = [NSMutableString stringwithCString:devSID];

    // etc...

    [pool release];
}
肩上的翅膀 2024-10-12 21:24:16

尝试这样的事情

[[NSString stringWithCString:"Hello"] keep];

keep 是一种评估最初未创建的对象的对象所有权的方法,因此这扩展了我们将每个分配与释放或自动释放相匹配的规则

注意:如果保留此对象,则必须平衡释放计数。如果您不释放保留的对象,那么您将面临内存泄漏问题。

Try something like this

[[NSString stringWithCString:"Hello"] retain];

retain is a way to assess object ownership on objects that you didn't initially create, so this extends our rule of matching every alloc with a release or autorelease

Note: You have to balance your release count if you retain this object. If you will not release retained object then you will face memory leak problem.

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