类方法中的IOS内存泄漏

时间:2021-12-08 23:43:24

In your opinion if I have a singleton subclass of NSObject being initialised with parameters like this:

在您看来,如果我有一个单例的NSObject子类被初始化为这样的参数:

- (MyObject *) initWithSomeParam:(NSString *)param{
    self = [super init];
    if (SharedInstance == nil){
        SharedInstance = [super init];
        SharedInstance.someProperty = param;
    }
    return self;
}

+ (MyObject *) objectWithSomeParam:(NSString *)param{
    return [[self alloc] initWithSomeParam:param];
    // Will the alloc cause a leak?
}

The user doesn't have access to the instance method, just the class. Thanks.

用户不能访问实例方法,只能访问类。谢谢。

1 个解决方案

#1


3  

That's not the normal way of implementing a singleton and you are breaking the convention of init. Better would be to create a sharedInstance class method and leave the initWithParam method to be more conventional:

这不是实现单例对象的常规方法,而且您违反了init的约定。最好是创建一个sharedInstance类方法,让initWithParam方法更常规:

static MyObject *_sharedInstance = nil;

+ (MyObject *)sharedInstance:(NSString *)param
{
    if (_sharedInstance == nil)
    {
        _sharedInstance = [MyObject alloc] initWithParam:param];
    }
    return _sharedInstance;
}

// This must be called during app termination to avoid memory leak
+ (void)cleanup
{
    [_sharedInstance release];
    _sharedInstance = nil;
}

- (id)initWithParam:(NSString *)param
{
    self = [super init];
    if (self != nil)
    {
        self.someProperty = param;
    }
    return self;
}

However, even that doesn't seem very comfortable; i.e. what happens if the user calls sharedInstance with a different parameter? Perhaps you want to keep a NSMutableDictionary of the initialized objects and create/return them depending on the parameter?

然而,即使是这样也不太舒服;例如,如果用户使用不同的参数调用sharedInstance会发生什么?也许您希望保留初始化对象的NSMutableDictionary并根据参数创建/返回它们?

If so, you would do:

如果是的话,你会这么做:

static NSMutableDictionary _sharedInstances = [[NSMutableDictionary alloc] init];

+ (MyObject *)sharedInstance:(NSString *)param
{
    MyObject *obj = [_sharedInstances objectForKey:param];
    if (obj == nil)
    {
        obj = [[MyObject alloc] initWithParam:param];
        [_sharedInstances setObject:obj forKey:param];
    }
    return obj;
}

// This must be called during app termination to avoid memory leak
+ (void)cleanup
{
    [_sharedInstances release];
    _sharedInstances = nil;
}

#1


3  

That's not the normal way of implementing a singleton and you are breaking the convention of init. Better would be to create a sharedInstance class method and leave the initWithParam method to be more conventional:

这不是实现单例对象的常规方法,而且您违反了init的约定。最好是创建一个sharedInstance类方法,让initWithParam方法更常规:

static MyObject *_sharedInstance = nil;

+ (MyObject *)sharedInstance:(NSString *)param
{
    if (_sharedInstance == nil)
    {
        _sharedInstance = [MyObject alloc] initWithParam:param];
    }
    return _sharedInstance;
}

// This must be called during app termination to avoid memory leak
+ (void)cleanup
{
    [_sharedInstance release];
    _sharedInstance = nil;
}

- (id)initWithParam:(NSString *)param
{
    self = [super init];
    if (self != nil)
    {
        self.someProperty = param;
    }
    return self;
}

However, even that doesn't seem very comfortable; i.e. what happens if the user calls sharedInstance with a different parameter? Perhaps you want to keep a NSMutableDictionary of the initialized objects and create/return them depending on the parameter?

然而,即使是这样也不太舒服;例如,如果用户使用不同的参数调用sharedInstance会发生什么?也许您希望保留初始化对象的NSMutableDictionary并根据参数创建/返回它们?

If so, you would do:

如果是的话,你会这么做:

static NSMutableDictionary _sharedInstances = [[NSMutableDictionary alloc] init];

+ (MyObject *)sharedInstance:(NSString *)param
{
    MyObject *obj = [_sharedInstances objectForKey:param];
    if (obj == nil)
    {
        obj = [[MyObject alloc] initWithParam:param];
        [_sharedInstances setObject:obj forKey:param];
    }
    return obj;
}

// This must be called during app termination to avoid memory leak
+ (void)cleanup
{
    [_sharedInstances release];
    _sharedInstances = nil;
}