这是正确的方法吗?
// convert
const void *buffer = NULL;
size_t size = 0;
dispatch_data_t new_data_file = dispatch_data_create_map(data, &buffer, &size);
if(new_data_file){ /* to avoid warning really - since dispatch_data_create_map demands we care about the return arg */}
NSData *nsdata = [[NSData alloc] initWithBytes:buffer length:size];
// use the nsdata... code removed for general purpose
// clean up
[nsdata release];
free(buffer); // warning: passing const void * to parameter of type void *
它运行良好。我主要担心的是内存泄漏。泄漏数据缓冲区并不好玩。那么 NSData、缓冲区和dispatch_data_t new_data_file 都好吗?
从我能读到的内容来看http://opensource.apple.com/source/libdispatch/libdispatch-187.7/dispatch/data.c看来缓冲区是 DISPATCH_DATA_DESTRUCTOR_FREE。这是否意味着我有责任释放缓冲区?
自 iOS 7 和 macOS 10.9 (基金会发行说明) dispatch_data_t
是一个 NSObject (NSObject <OS_dispatch_data>
)在 64 位应用程序中。
dispatch_data_t
现在可以自由投射到NSData *
, 尽管反之则不然.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)