停止ARC在空闲状态下释放对象 [英] Stop ARC releasing an object while it is idle

查看:79
本文介绍了停止ARC在空闲状态下释放对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在目标C中的内存管理有问题.我一直在阅读

I am having a problem with memory management in objective C. Ive been reading through the Advanced Memory Management Programming Guide but I cannot find a solution to my problem with the possible exception of abandoning ARC altogether and managing the memory manually.

问题出在这里:

我有一个已经创建的Controller类,其中包含有关在特定时间做什么的信息.

I have a Controller class that Ive made, that holds information on what to do at specific times.

Controller类告诉应用程序的其余部分播放视频(例如).视频播放正常.视频播放完后,Controller类知道下一步该怎么做.

The Controller class tells the rest of the app to play a video (for example). The video plays fine. When the video finishes playing, the Controller class knows what to do next.

不幸的是,几乎在视频开始播放后,Controller类就被ARC释放并重新分配.因此,在视频结束时,应用程序会调用Controller类以查看下一步应执行的操作,整个过程都会崩溃.我得到一个EXC_BAD_ACCESS,因为该类不再在内存中.

Unfortunately the Controller class is released and deallocated by ARC almost as soon as the video starts playing. So by the time the video ends, the app calls the Controller class to see what it should do next, and the whole thing crashes. I get an EXC_BAD_ACCESS because the class is no longer in memory.

我知道ARC正在释放我的Controller类,因为它告诉视频开始播放后,它什么也没做.但我想保留该课程,直到我再次需要它为止.

I get that ARC is releasing my Controller class because after it has told the video to start playing, its not doing anything. But I want to keep hold of that class until I need it again.

我将此类声明为属性,就像这样:

I am declaring this class as a property, like so:

@property (strong, nonatomic) Controller * controller;

但是,尽管如此,ARC仍然在不做任何事情的情况下立即释放该类.

But despite this, ARC keeps releasing the class as soon as its not doing anything.

我已经将此属性移到了App Delegate中.但是ARC仍在发布它.我无法将其转变为单例,因为我需要拥有该类的多个副本.

Ive moved this property into the App Delegate. But ARC is still releasing it. I cant turn this into a Singleton, as I need the potential to have multiple copies of this class.

这有可能吗?还是我应该放弃ARC而只手动进行内存管理?

Is this possible at all? Or should I abandon ARC and just do memory management manually?

推荐答案

使用单例模式,以便Controller照顾自己的生命周期并存在于整个应用程序范围内.从首次请求该共享实例开始,直到该应用终止,并且ARC不会任意释放它.

Use a singleton pattern so that Controller looks after its own lifetime and exists app-wide. This shared instance will exist from when it's first requested until the app terminates and ARC will not release it arbitrarily.

Controller.h:

Controller.h:

@interface Controller : NSObject

+ (Controller *)sharedInstance;

@end

Controller.m:

Controller.m:

#import "Controller.h"

static Controller *_instance = nil;
static dispatch_once_t _onceToken = 0;

@implementation Controller

+ (Controller *)sharedInstance {
    dispatch_once(&_onceToken, ^{
        _instance = [[Controller alloc] init];
    });
    return _instance;
}

// You could add this if you want to explicitly destroy the instance:
+ (void)destroy {
    _instance = nil;
    _onceToken = 0;
}

这篇关于停止ARC在空闲状态下释放对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆