简体   繁体   English

这是内存泄漏吗?

[英]Is this a memory leak?

I have something similar to this. 我有类似的东西。

initMyclass {
 if (self= [super init]) {
   classMember = [[NSMutableArray alloc] init];
 }
 return self;
}

Instruments reports a leak there. 仪器报告泄漏。

I'm leaking memory there ? 我在那里泄漏记忆? If not, xcode reports false memory leaks ? 如果没有,xcode报告错误的内存泄漏?

Thanks. 谢谢。

Instruments is reporting the leak there because you're not releasing the object elsewhere. 仪器报告那里的泄漏,因为你没有在其他地方发布物体。 You need to have [classMember release] in that class's dealloc method: 你需要在该类的dealloc方法中有[classMember release]

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

This is why you should use properties or explicit accessors. 这就是您应该使用属性或显式访问器的原因。

If you had this: 如果你有这个:

@interface myObject : NSObject
{
    NSMutableArray *classMembers;
}
@property(nonatomic, retain)  NSMutableArray *classMembers;

@end

@implementation myObject
@synthesize classMembers;

-(id) init{
    if (self=[super init]) {
        self.classMembers=[[NSMutableArray alloc] initWithCapacity:1];
    }
    return self;
}//-------------------------------------(id) init------------------------------------

-(void) dealloc{
    [classMembers release];
    [super dealloc];
}//-------------------------------------(void) dealloc------------------------------------

@end

You would not (and should not ever) have to miss around with the retention of a property. 你不会(也不应该)不得不错过保留财产。 This eliminates all leaks and over-releasing of properties. 这消除了所有泄漏和过度释放的特性。

If the property's object leaks, then you know automatically it is being retained in another object other than the instance of the class containing the property. 如果属性的对象泄漏,那么您自动知道它被保留在除包含该属性的类的实例之外的另一个对象中。

Is it a class or instance member? 它是实例成员吗? Show us the @interface for this class. 向我们展示这个类的@interface

Also, initMyClass .. or something .. is not a proper init method. 此外, initMyClass ..或某些东西..不是一个合适的init方法。 It's signature should be of the form: 它的签名应该是以下形式:

- (id) init {
    if ((self = [super init]) != nil) {
        someInstanceVariable = [NSMutableArray new];
    }
    return self;
}

Be more specific when you ask a question here. 当你在这里提问时更具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM