标签: automatic-ref-counting

NSWindowController窗口?

我有一个基于菜单栏的应用程序,在单击图标时显示一个窗口.这一切在Mac OS X Lion上运行良好,但由于某种原因,Snow Leopard上的错误发生在较早版本的Mac OS X上.随时[TheWindowController window]调用该方法停止,但应用程序仍在运行.因此,我不认为窗口只是零,它在某种程度上是腐败的.

我不知道为什么会发生这种情况,就像我说的那样,它只发生在Mac OS X Snow Leopard中.顺便说一句.我使用ARC,如果这很重要的话.

cocoa window nswindowcontroller automatic-ref-counting osx-lion

1
推荐指数
1
解决办法
1331
查看次数

UIViewController不在ARC项目中调用dealloc?

在ARC的项目,我addObserver在一个通知viewDidLoad:,并removeObserver:dealloc.但是在我弹出viewController后,dealloc没有执行.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshData)
                                                 name:MyNotification
                                               object:nil];
}

- (void)dealloc
{
    NSLog(@"==================");//There is nothing print out.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MyNotification
                                                  object:nil];
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

iphone objective-c automatic-ref-counting

1
推荐指数
1
解决办法
2211
查看次数

glCreateProgram 导致段错误?

我正在尝试用 Objective-C 编写一个 Wavefront OBJ 文件查看器,它能够从文件加载网格/材质/着色器。我已经为着色器和着色器程序创建了类,并且我正在尝试创建一个 OpenGL 着色器程序对象作为着色器程序类的 init 方法的一部分:

- (id)initWithVertexShader:(NSString *)vshader FragmentShader:(NSString *)fshader {
self = [super init];
if (self) {
    SRShader* shaders[2] = {
        [[SRShader alloc] initWithFilename:vshader Type:GL_VERTEX_SHADER Source:nil],
        [[SRShader alloc] initWithFilename:fshader Type:GL_FRAGMENT_SHADER Source:nil]
    };

    program = glCreateProgram();

    for (int i = 0; i < 2; i++) {
        SRShader* s = shaders[i];
        NSError* e = nil;
        s.source = [NSString stringWithContentsOfFile:s.filename encoding:NSUTF8StringEncoding error:&e];
        if (!e) {
            NSLog(@"Failed to read shader file: %@\n", s.filename);
            exit(-1);
        }

        GLuint shader = …
Run Code Online (Sandbox Code Playgroud)

opengl macos objective-c automatic-ref-counting

1
推荐指数
1
解决办法
1876
查看次数

使用ARC,我是否需要手动管理内存?

我的问题基本上就是标题.在启用了自动引用计数的XCode中,我是否需要手动管理内存?比如调用release,retain等?

谢谢!

xcode objective-c automatic-ref-counting

1
推荐指数
1
解决办法
91
查看次数

将现有单例升级为ARC

在Objective-C中,如何用ARC编写单例?在ARC中,不允许覆盖release,autorelease,retain,retainCount方法,如何避免释放对象?我知道没有ARC,下面是一个经典的单身人士:

@interface SingletonObject
+ (SingletonObject*)sharedObject;
@end

SingletonObject *sharedObj;

@implementation SingletonObject
+ (id)allocWithZone:(NSZone *)zone
{
    if (sharedObj == nil) {
        //So the code [[SingletonObject alloc] init] is equal with [SingletonObject sharedObject]
        sharedObj = [super allocWithZone:zone];
    }
    return sharedObj; 
}


+ (void)initialize
{
    if (self == [SingletonObject class]) {
        sharedObj = [[SingletonObject alloc] init];
    }
}

+ (SingletonObject*)sharedObject
{
    return sharedObj;
}
- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}

- …
Run Code Online (Sandbox Code Playgroud)

singleton objective-c automatic-ref-counting

1
推荐指数
1
解决办法
747
查看次数

UIAlertView或UIActionSheet如何处理在ARC下保留/释放自己?

我想为UIAlertView创建一个类似的类,它不需要强大的ivar.

例如,使用UIAlertView,我可以在我的一个UIViewController方法中执行以下操作:

UIAlertView *alertView     = [[UIActionSheet alloc] initWithTitle:nil
                                                          message:@"Foo"
                                                         delegate:nil
                                                cancelButtonTitle:@"Cancel"
                                                otherButtonTitles:nil];
[alertView show];
Run Code Online (Sandbox Code Playgroud)

...并且在不再可见之前不会释放actionSheet.

如果我要尝试做同样的事情:

MYAlertView *myAlertView = [[MYAlertView alloc] initWithMessage:@"Foo"];
[myAlertView show];
Run Code Online (Sandbox Code Playgroud)

... myAlertView实例将在我当前的方法结束时自动解除分配(例如,[myAlertView show]在行之后).

什么是防止这种情况发生的正确方法,而无需strong在我的UIViewController 上将myView声明为属性?(即我希望myView是一个局部变量,而不是一个实例变量,我希望MYAlertView实例负责自己的生命周期,而不是我的UIViewController控制它的生命周期.)

更新:MYAlertView继承自NSObject,因此无法添加到Views层次结构中.

objective-c automatic-ref-counting ios6

1
推荐指数
1
解决办法
943
查看次数

C回调中的ARC弱引用

我目前正在玩AudioQueue服务,我遇到了一个小问题.

AudioQueue有一堆回调,每个回调都可以携带一个"用户数据",基本上是一个指针.我希望我可以传递我的一个对象作为这个指针.

所以会发生的事情是,在某些情况下,AudioQueue以接近这个的方式调用我的回调:

static void HandleOutputBuffer (
    void                *aqData,
    AudioQueueRef       inAQ,
    AudioQueueBufferRef inBuffer
) {
    MyPlayerData *mpd = (MyPlayerData *)aqData;
    ...
}
Run Code Online (Sandbox Code Playgroud)

这通常很有效,但是当我的播放器到达媒体的末尾时,它会被取消分配.但通常HandleOutputBufferMyPlayerData对象被释放后调用回调,从而产生一个很好的段错误.

我希望我可以使用弱参考.有没有办法让我有一个ARC void *指针?每当对象被释放时,将被设置为nil的东西?

我目前正在看__weak,但我不确定这是使用的正确工具......

objective-c ios automatic-ref-counting

1
推荐指数
1
解决办法
425
查看次数

Dealloc没有用ARC调用子视图

我有一个不常见的架构.有一个UIViewController和许多子视图.我使用UIView,如viewController和UIViewController进行过渡动画.

UIView有很多子视图,我在主窗口上推,我removeFromSuperview并且一切正常,但是子视图不能调用dealloc方法.我也尝试清理对象

- (void) dealloc {
    NSLog(@"ActivityShowVC dealloc");
    [showView removeFromSuperview];
    showView = nil;
}

这叫.

- (void) dealloc {
    NSLog(@"ActivityShowView dealloc");

    [mainScroll removeFromSuperview];

    [titleView setDelegate:nil];
    [titleView removeFromSuperview];
    titleView = nil;

    [photoView setDelegate:nil];
    [photoView removeFromSuperview];
    photoView = nil;

    [predictionView setDelegate:nil];
    [predictionView removeFromSuperview];
    predictionView = nil;

    [calendarView setDelegate:nil];
    [calendarView removeFromSuperview];
    calendarView = nil;

    [descriptionView setDelegate:nil];
    [descriptionView removeFromSuperview];
    descriptionView = nil;
}

但这有一些问题.

objective-c uiviewcontroller uiview ios automatic-ref-counting

1
推荐指数
1
解决办法
6273
查看次数

为什么使用NSNotificationCenter而不是UIView.animateWithDuration可能会有强大的参考周期?

对于NSNotificationCenter块,我必须使用[unown self]避免强引用周期:

NSNotificationCenter.defaultCenter()
    .addObserverForName(UIApplicationWillEnterForegroundNotification,
        object: nil,
        queue: nil,
        usingBlock: { [unowned self] (notification : NSNotification!) -> Void in
            self.add(123)
        })
Run Code Online (Sandbox Code Playgroud)

但是,在UIView.animateWithDuration中,我不必使用[unown self]:

   UIView.animateWithDuration(0.5, animations: { () -> Void in
      self.someOutlet.alpha = 1.0
      self.someMethod()
   })
Run Code Online (Sandbox Code Playgroud)

有什么不同?

ios automatic-ref-counting strong-references swift

1
推荐指数
1
解决办法
854
查看次数

使用单例模式时,我的公共类应该返回私有还是公共实例?

我有一个像这样定义的单例:

public partial class MoonDataManager
{

    static MoonDataManager _singletonInstance;
    public static MoonDataManager SingletonInstance
    {
        get
        {
            return _singletonInstance;
        }
        private set
        {
            _singletonInstance = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我有一个安全地创建实例的函数:

   public static async Task<MoonDataManager> CreateSingletonAsync()
    {
            _singletonInstance = new MoonDataManager();
Run Code Online (Sandbox Code Playgroud)

我是不是该:

return  _singletonInstance;  (field) 
Run Code Online (Sandbox Code Playgroud)

要么

return SingletonInstance;  (property)
Run Code Online (Sandbox Code Playgroud)

我关心垃圾收集,特别是在Xamarin的iOS或Android中.

如果在C#中有这样的命名模式,请告诉我是否偏离了标准.


更新:

现在我觉得我真的陷入了线程和异步方法的困境.以下是对象及其目标:

  • MoonDataManager:RegisterTable<Models.IssuerKey>每个表运行一次.这是一种基本上运行的通用方法(new MobileServiceSQLiteStore).DefineTable<T>()

  • OfflineStore:这是一个MobileServiceSQLiteStore.

  • MobileClient:这是一个MobileServiceClient.

  • MoonDataManager依赖关系:MoonDataManager要求OfflineStore和MobileClient完成初始化.具体来说,它执行MobileServiceClient.SyncContext.InitializeAsync(OfflineStore)

我不确定如何理解这种依赖关系的意义...或者如何使代码看起来很好,并且是线程安全的.

这是代码的新迭代:

private readonly Lazy<MobileServiceClient> lazyMobileClient = 
        new Lazy<MobileServiceClient>(() => …
Run Code Online (Sandbox Code Playgroud)

c# garbage-collection xamarin.ios xamarin.android automatic-ref-counting

1
推荐指数
1
解决办法
985
查看次数