您的位置:首页 > 其它

AsyncSocket第三方开源库经常会在release环境下出现接收消息闪退

2016-03-08 13:43 387 查看
自从Xcode7出来以后,项目中使用的AsyncSocket第三方开源库经常会在release环境下出现接收消息闪退等现象,但是在debug环境下面则没有相关的问题,导致很难找到问题所在以及解决方法。在花费了很长时间查找资料以及相关问题搜索以后,发现有可能是因为在Xcode7在打包的时候,Xcode会自动精简代码块,导致问题某些代码在release环境下出现内存管理不到位而抛野指针,出现闪退现象。

在经过大量修改和测试以后,发现修改AsyncSocket开源库中的AsyncSocket.m的以下代码,闪退问题不在出现,修改前:

/**
* This is the callback we setup for CFReadStream.
* This method does nothing but forward the call to it's Objective-C counterpart
**/
static void MyCFReadStreamCallback (CFReadStreamRef stream, CFStreamEventType type, void *pInfo)
{
@autoreleasepool {

AsyncSocket *theSocket = (__bridge AsyncSocket *)pInfo;
[theSocket doCFReadStreamCallback:type forStream:stream];

}
}

/**
* This is the callback we setup for CFWriteStream.
* This method does nothing but forward the call to it's Objective-C counterpart
**/
static void MyCFWriteStreamCallback (CFWriteStreamRef stream, CFStreamEventType type, void *pInfo)
{
@autoreleasepool {

AsyncSocket *theSocket = (__bridge AsyncSocket *)pInfo;
[theSocket doCFWriteStreamCallback:type forStream:stream];

}
}


修改后的代码如下:

static void MyCFReadStreamCallback (CFReadStreamRef stream, CFStreamEventType type, void *pInfo)
{
@autoreleasepool {

AsyncSocket *theSocket =[[AsyncSocket alloc]init];
theSocket =  (__bridge AsyncSocket *)pInfo;
[theSocket doCFReadStreamCallback:type forStream:stream];

}
}

static void MyCFWriteStreamCallback (CFWriteStreamRef stream, CFStreamEventType type, void *pInfo)
{
@autoreleasepool {
AsyncSocket *theSocket =[[AsyncSocket alloc]init];
theSocket = (__bridge AsyncSocket *)pInfo;
[theSocket doCFWriteStreamCallback:type forStream:stream];

}
}

修改的核心是:为接收消息时的对象重新开辟一下内存,让其不会被提前释放而抛出野指针。

以上只是我个人的理解,有说错的或者不对的地方,希望大家能理解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: