0

sharedInstance我用下面的代码创建了一个:

 + (MyClass *)sharedStore
    {
        @synchronized(self) 
       {
            if (sharedInstance == nil)
               sharedInstance = [[self alloc] init];
        }
        return sharedInstance;
    }

我有一个功能MyClass

- (MyClass *)initService:(int)serviceType areaSelected :(int)areaID target:(id)delegate
{

    self = [super init];
    // again init function is called after having a shared instance which is resulting in memory leak.

    if(self)
    {
        jsonParserObject = [[MyClassInfoParser alloc]init];
        if(jsonParserObject != nil)
        {
            serviceTypeRequested = serviceType;

                selectedAreaId = areaID;
                NSString *url = [self createUrlandBodyForService];
                NSLog(@"URL : %@",url);
                if ([Reachability connected])
                {
                    if(url.length)
                    {
                        BOOL status =  [self initRequest:url withDelegate:delegate];
                        if(status)
                            NSLog(@"Request for fields successfully");
                        else
                            NSLog(@"Failed to send request for fields");
                    }
                    else
                    {
                         [super responseFailedNotification:nil];
                    }
                }
            else
            {
                [super networkError:delegate];
            }

        }

    }
    return self;
}

要调用上述函数,我使用代码

   - (void)startServiceForInformation
    {
        serviceObj = [[MyClass sharedStore]initService:2 areaSelected:self.areaId target:self];
        [serviceObj start];

    }

这导致内存泄漏导致崩溃。谁能帮我解决这个内存泄漏?

4

2 回答 2

1

好吧,为什么在使用单例类时要使用另一个 init 方法?不需要第二个 init 方法使其成为一个方法,只需使用您的单例实例调用它

尝试这个

- (void)startService:(int)serviceType areaSelected :(int)areaID target:(id)delegate
{
        jsonParserObject = [[MyClassInfoParser alloc]init];
        if(jsonParserObject != nil)
        {
            serviceTypeRequested = serviceType;

            selectedAreaId = areaID;
            NSString *url = [self createUrlandBodyForService];
            NSLog(@"URL : %@",url);
            if ([Reachability connected])
            {
                if(url.length)
                {
                    BOOL status =  [self initRequest:url withDelegate:delegate];
                    if(status)
                        NSLog(@"Request for fields successfully");
                    else
                        NSLog(@"Failed to send request for fields");
                }
                else
                {
                    [super responseFailedNotification:nil];
                }
            }
            else
            {
                [super networkError:delegate];
            }
        }
}

并打电话

- (void)startServiceForInformation
    {
        serviceObj = [[MyClass sharedStore]startService:2 areaSelected:self.areaId target:self];
        [serviceObj start];

    }
于 2013-05-15T04:43:23.703 回答
1

没有必要在第二个函数中调用 [super init] ,假设它们都在同一个类上,这是我根据您的评论推断出来的。

由于这是一个实例函数,而不是像您的第一个函数那样的静态函数,因此您在对象上调用它。在大多数情况下,您正在覆盖NSObject init,您有一个由类的选择器创建的相对裸露的对象alloc

在这种情况下,您只是在配置一个已经创建的对象。你确定你需要单例接口吗?

于 2013-05-15T04:45:55.843 回答