Objective-C Library - cannot form weak reference to instance of class

前端 未结 3 645
借酒劲吻你
借酒劲吻你 2021-02-08 11:38

I\'m currently working with the XMPP Library for Objective-C, and I\'m using the \"Desktop\" example code.

It logs in fine; however, when I open a new chat, or someone s

3条回答
  •  失恋的感觉
    2021-02-08 11:43

    In my project (as a mistake) there was a weak reference to self in dealloc (it was a separate method, called to clear used resource). Using weak reference to one property of this object (that captured just a reference to the resource) solved the problem.

    It is really strange to create weak reference to half-destroyed object in dealloc.

    NEVER WRITE LIKE THIS:

    - (void) dealloc
    {
        [self freeUsedResource];
    }
    
    - (void) freeUsedResource
    {
        __weak MyClass *weakSelf = self;
        dispatch_async(self.queue, ^{
    
            [weakSelf.usedResource freeUsedMemory];
        });
    }
    

提交回复
热议问题