CFStreamCreatePairWithSocketToCFHost 中的内存泄漏
我正在转换可用的 SKP_SMTP 库以在我的 iPhone 应用程序中实现 SMTP 客户端。经过漫长而麻烦的错误修复和内存检查工作后,我将问题范围缩小到只有一个。在调用 CFStreamCreatePairWithSocketToCFHost 时发生内存泄漏(由 Instruments 中的 Leaks 工具证明),这对我来说似乎很奇怪,因为 CFReadStreamRef 参数 readStream< /strong> 和 writeStream 要么在稍后阶段 CFReleased ,要么通过 __bridge_transfer 分配给相应的 NSStream 对象(这最终应该将流内存管理从 CoreFoundation 带到 ARC)。 我已经用谷歌搜索过,但我发现的唯一解决方案是在垃圾收集下(显然在 ARC 中不可用):我是否遗漏了某些东西或者不应该发生的情况? 代码如下, 多谢
@implementation NSStream (SKPSMTPExtensions)
+ (void)getStreamsToHostNamed:(NSString *)hostName port:(NSInteger)port inputStream: (NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream
{
CFHostRef host;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
readStream = NULL;
writeStream = NULL;
host = CFHostCreateWithName(NULL, (__bridge_retained CFStringRef) hostName);
if (host != NULL)
{
// Memory leak at this line: strange as readStream & writeStream are either CFReleased or _bridge_transfered to ARC
(void) CFStreamCreatePairWithSocketToCFHost(NULL, host, port, &readStream, &writeStream);
CFRelease(host);
}
if (inputStream == NULL)
{
CFRelease(readStream);
}
else
{
*inputStream = (__bridge_transfer NSInputStream *) readStream;
}
if (outputStream == NULL)
{
CFRelease(writeStream);
}
else
{
*outputStream = (__bridge_transfer NSOutputStream *) writeStream;
}
}
@end
I am in the process of converting the available SKP_SMTP library to implement an SMTP client in my iPhone app. After a long and troubled work of bug fixing and memory checks, I have narrowed down the problems to only one. It happens that a memory leaks occurs in the call to CFStreamCreatePairWithSocketToCFHost (evidentiated by the Leaks tool in Instruments), which seems strange to me as the CFReadStreamRef parameters readStream and writeStream are either CFReleased at a later stage, or assigned to a correspondent NSStream object via __bridge_transfer (and that should eventually bring the streams memory management from CoreFoundation to ARC).
I've googled around but the only solutions I have found are under garbage collection(clearly not available in ARC): am I missing something or that should not happen?
Code following,
thanks a lot
@implementation NSStream (SKPSMTPExtensions)
+ (void)getStreamsToHostNamed:(NSString *)hostName port:(NSInteger)port inputStream: (NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream
{
CFHostRef host;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
readStream = NULL;
writeStream = NULL;
host = CFHostCreateWithName(NULL, (__bridge_retained CFStringRef) hostName);
if (host != NULL)
{
// Memory leak at this line: strange as readStream & writeStream are either CFReleased or _bridge_transfered to ARC
(void) CFStreamCreatePairWithSocketToCFHost(NULL, host, port, &readStream, &writeStream);
CFRelease(host);
}
if (inputStream == NULL)
{
CFRelease(readStream);
}
else
{
*inputStream = (__bridge_transfer NSInputStream *) readStream;
}
if (outputStream == NULL)
{
CFRelease(writeStream);
}
else
{
*outputStream = (__bridge_transfer NSOutputStream *) writeStream;
}
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这个语句泄漏了
hostName
:您应该使用
__bridge
强制转换,而不是__bridge_retained
强制转换。I think this statement leaks
hostName
:You should use a
__bridge
cast, not a__bridge_retained
cast.