内存泄漏,MutableArray中的对象没有被释放?

发布于 2024-11-26 06:22:22 字数 1903 浏览 0 评论 0原文

Xcode 告诉我下面的代码在内存泄漏方面存在一些问题。

@property (nonatomic, retain) NSMutableArray *naw_rows;

-(void) loadTableRows:(BOOL)shouldReload
{
    [naw_rows removeAllObjects];

  [self.naw_rows addObject: [[CellModel alloc] initialize:@"name"     title:@"Name"          value: self.currentProfile.name]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"company"  title:@"Company name"  value: self.currentProfile.company]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"address"  title:@"Address"         value: self.currentProfile.address]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"zipcode"  title:@"Zipcode"      value: self.currentProfile.zipcode]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"city"     title:@"City"        value: self.currentProfile.city]];
}

// here is my cellModel object:
@implementation CellModel

-(id) initialize:(NSString *)newName title:(NSString *)newTitle value:(NSString *)newValue;
{
    if (self == [super init])
    {
        name = newName;
        title = newTitle;
        value = newValue;
    }
    return self;
}

- (NSString *) getName
{
   return name;
}

- (NSString *) getTitle
{
    return title;
}

- (NSString *) getValue
{
    return value;
}

-(void)dealloc
{
    [super dealloc];
}

@end;

所有 addObject 行都会出现以下错误:

在线分配的对象的潜在泄漏 -- 方法返回一个 具有 +1 保留计数(拥有引用)的 Objective-C 对象 在线分配——稍后在此执行路径中不会引用 并且保留计数为+1(对象泄漏)

在有关内存泄漏的其他主题中,我发现这将是执行此操作的正确方法:

CellModel *model = [[CellModel alloc] initialize:@"name" title:@"Name" value: self.currentProfile.name];
[self.naw_rows addObject: model];
[model release];

但这给了我以下错误:

不正确减少对象的引用计数 此时由调用者拥有

那么我做错了什么?在我的第一段代码中,保留计数应该为 1。由数组拥有。我假设当我使用 [array remodeAllObjects] 时对象被释放,

提前致谢,

Nico

Xcode is telling me that there are some problems with the code below in terms of memory leaking.

@property (nonatomic, retain) NSMutableArray *naw_rows;

-(void) loadTableRows:(BOOL)shouldReload
{
    [naw_rows removeAllObjects];

  [self.naw_rows addObject: [[CellModel alloc] initialize:@"name"     title:@"Name"          value: self.currentProfile.name]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"company"  title:@"Company name"  value: self.currentProfile.company]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"address"  title:@"Address"         value: self.currentProfile.address]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"zipcode"  title:@"Zipcode"      value: self.currentProfile.zipcode]];
  [self.naw_rows addObject: [[CellModel alloc] initialize:@"city"     title:@"City"        value: self.currentProfile.city]];
}

// here is my cellModel object:
@implementation CellModel

-(id) initialize:(NSString *)newName title:(NSString *)newTitle value:(NSString *)newValue;
{
    if (self == [super init])
    {
        name = newName;
        title = newTitle;
        value = newValue;
    }
    return self;
}

- (NSString *) getName
{
   return name;
}

- (NSString *) getTitle
{
    return title;
}

- (NSString *) getValue
{
    return value;
}

-(void)dealloc
{
    [super dealloc];
}

@end;

All the addObject lines give the following error:

Potential leak of an object allocated on line -- Method returns an
Objective-C object with a +1 retain count (owning reference) Object
allocated on line -- is not referenced later in this execution path
and has a retain count of +1 (object leaked)

In other topics about memory leaking i found that this would be the correct way to do this:

CellModel *model = [[CellModel alloc] initialize:@"name" title:@"Name" value: self.currentProfile.name];
[self.naw_rows addObject: model];
[model release];

But this gives me the following error:

Incorrect decrement of the reference count of an object that is not
owned at this point by the caller

So what am i doing wrong? In my first piece of code the retain count should be 1. Owned by the array. And i assume the objects are released when i use [array remodeAllObjects]

Thanks in advance,

Nico

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

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

发布评论

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

评论(2

始终不够爱げ你 2024-12-03 06:22:22

这会泄漏:

[self.naw_rows addObject: [[CellModel alloc] initialize:@"name"     title:@"Name"          value: self.currentProfile.name]];

您拥有 alloc-init 返回的对象,并且您有责任通过向其发送 releaseautorelease 消息来放弃对该对象的所有权,但您没有这样做。

正如您所建议的,使用临时变量确实可以解决问题:

CellModel *model = [[CellModel alloc] initialize:@"name" title:@"Name" value: self.currentProfile.name];
[self.naw_rows addObject: model];
[model release];

那么,为什么分析器会抱怨呢?因为你的初始化程序的名称。将其重命名为类似 initWithName:title:value: 的内容,您将看到“调用者此时不拥有的对象的引用计数的错误递减”消失了。

约定是初始化方法的名称应以缩写 init 开头。

此外,类的实现不会将 self 分配给调用 super 初始化程序的结果。这个:

if (self == [super init])

应该是:

if ((self = [super init]))

或者,如果您愿意的话:

self = [super init];
if (self)

另外,CellModel 类的实例变量的内存管理是错误的。您应该保留或复制作为参数传递给 init 方法的对象,并在 dealloc 中释放它们。

访问器方法还打破了 命名约定,它们应该简单命名为nametitle等。前缀“get”仅适用于间接返回对象的方法。

This leaks:

[self.naw_rows addObject: [[CellModel alloc] initialize:@"name"     title:@"Name"          value: self.currentProfile.name]];

You own the object returned by alloc-init and you are responsible for relinquish ownership of it by sending it a release or autorelease message, which you fail to do.

Using a temporary variable, as you propose, does solve the problem:

CellModel *model = [[CellModel alloc] initialize:@"name" title:@"Name" value: self.currentProfile.name];
[self.naw_rows addObject: model];
[model release];

So, why does the analyzer complain? Because of the name of your initializer. Rename it to something like initWithName:title:value: and you'll see the "Incorrect decrement of the reference count of an object that is not owned at this point by the caller" go away.

The convention is that the name of initializer methods should begin with the abbreviation init.

Also, the implementation of your class does not assign self to the result of calling super's initializer. This:

if (self == [super init])

should be:

if ((self = [super init]))

or, if you prefer:

self = [super init];
if (self)

Also, the memory management of the instance variables of the CellModel class is wrong. You should retain or copy the objects passed as arguments to your init method and also release them in dealloc.

The accessor methods also break the naming conventions, they should be named simply name, title, etc. The prefix "get" is only for methods that return objects indirectly.

翻了热茶 2024-12-03 06:22:22

我相信这是因为 CellModel 拥有该对象。尽管您已经释放了它,但该对象还没有。要修复此问题,请添加代码以在 dealloc 方法中释放数组。

-(void)dealloc
{
    [self.naw_rows release];
    [super dealloc];
}

另外,作为旁注,您通常不应该依赖retainCount,因为它很容易产生误导。

I believe that this is because the CellModel owns the object. Although you have released it, the object has not. To fix this add code to release the array in the dealloc method.

-(void)dealloc
{
    [self.naw_rows release];
    [super dealloc];
}

Also, as a sidenote, you generally shouldn't rely on retainCount because it can easily be misleading.

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