iphone - 单例内存泄漏

标签 iphone objective-c

我有一个单例类,init 方法中的行导致内存泄漏,我不知道为什么......

这是我的实现

static timerController *sngTimer = nil;

@implementation timerController

@synthesize repeatingTimer;
@synthesize dateComp;
@synthesize bPause;

+(timerController *) singletonTimer
{
    @synchronized(self){
        if (sngTimer == nil ) 
        {
            sngTimer = [[timerController alloc]init];
        }
    }

    return sngTimer;
}

-(id)init
{
    self = [super init];

    if (self != nil) {

        dateComp = [[NSDateComponents alloc] init];         ///this line cause memory leak
        NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; ///this line cause memory leak

        [dateComp setCalendar:gregorianCalendar]; ///this line cause memory leak

        [gregorianCalendar release];

        bPause = FALSE;
    }

    return self;
}

最佳答案

我不知道你是如何检测泄漏的,但很可能你使用的任何工具都会注意到你没有 dealloc 方法来释放你在那里创建的对象。为了完整性,您可能应该拥有它,但只要该类仅用作单例,那么它并不重要。

关于iphone - 单例内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4158515/

相关文章:

iphone - 电视输出 iPhone 应用程序

javascript - 为什么在 iOS Safari Mobile (iPhone/iPad) 中不触发模糊事件?

iphone - 验证电话号码 ios

iphone - 绘制到 CGImageRef 中

objective-c - 以编程方式连接到蓝牙设备 objective c

objective-c - 从 IOUSBInterface 管道中找出数据

objective-c - 如何将我的 Swift 应用程序连接到我的 Parse Server?

ios - 从新应用版本中删除应用内购买项目

iphone - modalViewController 委托(delegate)回调 - 如何?

ios - UISwipeGestureRecognizer 干扰 slider