使用 NSArray ComponentsSeparatedByString 时发生内存泄漏

发布于 2024-12-01 13:07:20 字数 1633 浏览 1 评论 0原文

我正在使用 Leaks 模板运行 Instruments 应用程序,它告诉我线路出现泄漏:

NSArray *tempFavs = [tempFavString componentsSeparatedByString:@"|"];

我看到其他一些人也遇到类似的问题,但我还没有看到任何解决方案。看来这个数组应该是自动释放的,我不必担心它。我分配的所有数组都在 dealloc 方法中释放。这是所有相关代码:

NSArray *tempFavoritesArray = [appPreferences arrayForKey:[NSString stringWithFormat:@"%@ %@ favorites", server, project]];
favoritesArrayDisplay = [[NSMutableArray alloc] initWithObjects:nil];
cenXsArray = [[NSMutableArray alloc] initWithObjects:nil];
cenYsArray = [[NSMutableArray alloc] initWithObjects:nil];
viewScalesArray = [[NSMutableArray alloc] initWithObjects:nil];
currentPresetsArray = [[NSMutableArray alloc] initWithObjects:nil];
rastersArray = [[NSMutableArray alloc] initWithObjects:nil];
empty = NO;
selected = NO;

if ([tempFavoritesArray count] == 0 || tempFavoritesArray == nil)
{
    [favoritesArrayDisplay addObject:@"No favorites saved."];
    empty = YES;
}

for (int i=0; i<[tempFavoritesArray count]; i++)
{
    NSString *tempFavString = [NSString stringWithString:[tempFavoritesArray objectAtIndex:i]];
    NSArray *tempFavs = [tempFavString componentsSeparatedByString:@"|"];        

    if ([tempFavs count] > 2)
    {
        [favoritesArrayDisplay addObject:[tempFavs objectAtIndex:0]];
        [cenXsArray addObject:[tempFavs objectAtIndex:1]];
        [cenYsArray addObject:[tempFavs objectAtIndex:2]];
        [viewScalesArray addObject:[tempFavs objectAtIndex:3]];
        [currentPresetsArray addObject:[tempFavs objectAtIndex:4]];
        [rastersArray addObject:[tempFavs objectAtIndex:5]];
    }
}

以前有人见过这个吗?

I'm running the Instruments application with the Leaks template and it's telling me that I have a leak at the line:

NSArray *tempFavs = [tempFavString componentsSeparatedByString:@"|"];

I've seen some other people having similar problems but I haven't seen any solutions. It seems that this array should be autoreleased and I shouldn't have to worry about it. All of the arrays that I've alloc'd are released in the dealloc method. Here's all of the relevant code:

NSArray *tempFavoritesArray = [appPreferences arrayForKey:[NSString stringWithFormat:@"%@ %@ favorites", server, project]];
favoritesArrayDisplay = [[NSMutableArray alloc] initWithObjects:nil];
cenXsArray = [[NSMutableArray alloc] initWithObjects:nil];
cenYsArray = [[NSMutableArray alloc] initWithObjects:nil];
viewScalesArray = [[NSMutableArray alloc] initWithObjects:nil];
currentPresetsArray = [[NSMutableArray alloc] initWithObjects:nil];
rastersArray = [[NSMutableArray alloc] initWithObjects:nil];
empty = NO;
selected = NO;

if ([tempFavoritesArray count] == 0 || tempFavoritesArray == nil)
{
    [favoritesArrayDisplay addObject:@"No favorites saved."];
    empty = YES;
}

for (int i=0; i<[tempFavoritesArray count]; i++)
{
    NSString *tempFavString = [NSString stringWithString:[tempFavoritesArray objectAtIndex:i]];
    NSArray *tempFavs = [tempFavString componentsSeparatedByString:@"|"];        

    if ([tempFavs count] > 2)
    {
        [favoritesArrayDisplay addObject:[tempFavs objectAtIndex:0]];
        [cenXsArray addObject:[tempFavs objectAtIndex:1]];
        [cenYsArray addObject:[tempFavs objectAtIndex:2]];
        [viewScalesArray addObject:[tempFavs objectAtIndex:3]];
        [currentPresetsArray addObject:[tempFavs objectAtIndex:4]];
        [rastersArray addObject:[tempFavs objectAtIndex:5]];
    }
}

Has anyone seen this before?

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

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

发布评论

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

评论(3

隱形的亼 2024-12-08 13:07:20

所有泄漏都告诉您,由该行代码分配的一个或多个对象稍后会被泄漏。它没有向您显示导致实际泄漏的代码行,而是显示创建后来泄漏的分配的代码行。

即,您可能过度保留 tempFavs 数组中的字符串之一,并且泄漏将其识别为泄漏分配。

首先,尝试“构建和分析”。如果这不能解决问题,请使用分配工具准确找出泄漏的对象以及保留/释放的位置。

All leaks is telling you is that an object or objects allocated by that line of code are later leaked. It is not showing you the line of code that caused the actual leak, but the line of code that created the allocation that was later leaked.

I.e. you may be over-retaining one of the strings in the tempFavs array and leaks is identifying it as a leaked allocation.

First, try "build and analyze". If that doesn't fix the problem, use the Allocations instrument to figure out exactly which object was leaked and where it was retained/released.

伊面 2024-12-08 13:07:20

您说您分配的所有数组都在 dealloc 方法中释放,但是您确定正在调用此 dealloc 吗?也许您实际上泄漏了包含所有这些数组的对象。

You say that all of the arrays that you've alloc'd are released in the dealloc method, but are you positive that this dealloc is being called? Maybe you're actually leaking the object that contains all those arrays.

染柒℉ 2024-12-08 13:07:20

还没有看到有关 ComponentsSeparatedByString 增加保留计数的任何内容,但您确定没有在类中的其他位置保留任何 NSArray 吗?

Haven't seen anything about componentsSeparatedByString increasing the retain count, but you're sure you're not retaining any of the NSArray's somewhere else in the class?

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