在非ARC项目中释放对象,对象在ARC库中声明

Kri*_*dra 6 iphone release ios automatic-ref-counting

那么我正在研究一个非ARC项目,但是使用了使用ARC编写的Philipp Kyeck的socketio库.我正在使用教程中介绍的方法合并非ARC项目和ARC库.

在我的ViewController文件中,我正在使用初始化套接字

 SockIO *chatSockIO = [[SocketIO alloc] initWithDelegate:self];
Run Code Online (Sandbox Code Playgroud)

当我需要断开时,我打电话

[chatSockIO disconnect];
Run Code Online (Sandbox Code Playgroud)

这会导致socketIODidDisconnect委托方法触发.

- (void) socketIODidDisconnect:(SocketIO *)socket{
   [chatSockIO release]; ==> is this call needed?
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是关于这条线[chatSockIO release].我们是否需要释放一个本身在ARC模式下定义但在非ARC项目中使用的对象?

现在,当我尝试发布时,我得到了一个例外

-[SocketIO retain]: message sent to deallocated instance 0x6fec370
Run Code Online (Sandbox Code Playgroud)

但是当我注释掉那一行时,我的内存泄漏和dealloc在我的库对象中根本没有被调用

赏金时间!!

忘记我提到的库,崩溃我的代码和泄漏.. 在非ARC项目中使用使用ARC方法定义的对象时的常规做法是什么.我应该只分配它,还是应该在使用后分配和释放它?

编辑:更多信息.

我在崩溃时运行zombie instrument,这就是它所说的..它显示了对alloc和release函数的调用.

#   Address     Category    Event Type  RefCt   Timestamp       Size    Responsible Library     Responsible Caller
0   0x72d5da0   SocketIO    Malloc      1       00:09.700.274   64      MyProject           -[MyViewController sendRequestForSocketIOPush]
1   0x72d5da0   SocketIO    Retain      2       00:09.700.317   0       MyProject           -[SocketIO initWithDelegate:]
2   0x72d5da0   SocketIO    Release     1       00:09.700.320   0       MyProject           -[SocketIO initWithDelegate:]
3   0x72d5da0   SocketIO    Retain      2       00:09.700.440   0       Foundation          -[NSURLConnectionInternal initWithInfo:]
4   0x72d5da0   SocketIO    Retain      3       00:10.413.717   0       Foundation          -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]
5   0x72d5da0   SocketIO    Release     2       00:10.413.761   0       Foundation          -[NSURLConnectionInternalConnection invokeForDelegate:]
6   0x72d5da0   SocketIO    Retain      3       00:10.413.797   0       Foundation          -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]
7   0x72d5da0   SocketIO    Release     2       00:10.413.811   0       Foundation          -[NSURLConnectionInternalConnection invokeForDelegate:]
8   0x72d5da0   SocketIO    Retain      3       00:10.413.816   0       Foundation          -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]
9   0x72d5da0   SocketIO    Release     2       00:10.415.087   0       Foundation          -[NSURLConnectionInternalConnection invokeForDelegate:]
10  0x72d5da0   SocketIO    Retain      3       00:10.415.214   0       Foundation          -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]
11  0x72d5da0   SocketIO    Release     2       00:10.415.216   0       Foundation          -[NSURLConnectionInternalConnection invokeForDelegate:]
12  0x72d5da0   SocketIO    Release     1       00:10.415.275   0       Foundation          -[NSURLConnectionInternalConnection invokeForDelegate:]
13  0x72d5da0   SocketIO    Retain      2       00:10.969.432   0       GraphicsServices    GSEventRunModal
14  0x72d5da0   SocketIO    Release     1       00:10.969.433   0       GraphicsServices    GSEventRunModal
15  0x72d5da0   SocketIO    Retain      2       00:10.969.434   0       GraphicsServices    GSEventRunModal
16  0x72d5da0   SocketIO    Release     1       00:10.969.456   0       GraphicsServices    GSEventRunModal
17  0x72d5da0   SocketIO    Retain      2       00:10.969.459   0       GraphicsServices    GSEventRunModal
18  0x72d5da0   SocketIO    Retain      3       00:10.969.488   0       Foundation          -[NSCFTimer initWithFireDate:interval:target:selector:userInfo:repeats:]
19  0x72d5da0   SocketIO    Release     2       00:10.976.115   0       MyProject           -[SocketIO setTimeout]
20  0x72d5da0   SocketIO    Retain      3       00:10.976.125   0       Foundation          -[NSCFTimer initWithFireDate:interval:target:selector:userInfo:repeats:]
21  0x72d5da0   SocketIO    Release     2       00:10.976.161   0       GraphicsServices    GSEventRunModal
22  0x72d5da0   SocketIO    Retain      3       00:13.935.328   0       GraphicsServices    GSEventRunModal
23  0x72d5da0   SocketIO    Release     2       00:13.935.373   0       MyProject           -[SocketIO setTimeout]
24  0x72d5da0   SocketIO    Retain      3       00:13.935.399   0       Foundation          -[NSCFTimer initWithFireDate:interval:target:selector:userInfo:repeats:]
25  0x72d5da0   SocketIO    Release     2       00:13.935.685   0       MyProject           -[SocketIO onDisconnect]
26  0x72d5da0   SocketIO    Release     1       00:13.935.705   0       MyProject           -[MyViewController socketIODidDisconnect:]
27  0x72d5da0   SocketIO    Release     0       00:13.935.716   0       GraphicsServices    GSEventRunModal
28  0x72d5da0   SocketIO    Zombie      -1      00:13.936.298   0       GraphicsServices    GSEventRunModal
Run Code Online (Sandbox Code Playgroud)

Tam*_*ese 3

回答你的问题:

如果您从非 ARC 代码中使用 ARC 管理的对象,则可以像使用非 ARC 对象一样使用它:如果您创建或保留它,则必须释放或自动释放它。

关于您的问题:

在您的评论中,您提到您尝试通过像这样的初始化来解决问题

self.chatSockIO = [[[SocketIO alloc] initWithDelegate:self] autorelease];
Run Code Online (Sandbox Code Playgroud)

并在socketIODidDisconnect

self.chatSockIO = nil;
Run Code Online (Sandbox Code Playgroud)

如果该chatSockIO属性具有语义,并且一次retain仅使用一个对象,那么这应该可以正常工作。SocketIO

Zombie 输出给出了出现问题的提示:

  • 在倒数第二行中,释放对象,保留计数降至 0,并且对象被释放。这正是您所期望的。
  • 然而,在最后一行中,尝试从运行循环中保留该对象。你知道这是一个保留,因为你得到了没有的异常NSZombie
  • 这意味着尽管您已经完成了该对象,但它仍然会收到来自某个地方的调用。这是出乎意料的。

它可能是您的代码中的某些内容,也可能是您正在使用的库之一中的错误。只是预感:将SocketIO.m这些行替换为-onDisconnect

if (_webSocket != nil) {
    [_webSocket close];
}
Run Code Online (Sandbox Code Playgroud)

if (_webSocket != nil) {
    [_webSocket close];
    _webSocket.delegate = nil;
}
Run Code Online (Sandbox Code Playgroud)