相关疑难解决方法(0)

如何将dispatch_data_t转换为NSData?

这是正确的方法吗?

// 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 *
Run Code Online (Sandbox Code Playgroud)

它工作正常.我主要担心的是内存泄漏.泄漏数据缓冲区并不好玩.那么NSData,缓冲区和dispatch_data_t new_data_file都可以吗?

从我在http://opensource.apple.com/source/libdispatch/libdispatch-187.7/dispatch/data.c上可以看到,似乎缓冲区是DISPATCH_DATA_DESTRUCTOR_FREE.这是否意味着释放缓冲区是我的责任?

c nsdata dispatch grand-central-dispatch ios

8
推荐指数
2
解决办法
7403
查看次数

标签 统计

c ×1

dispatch ×1

grand-central-dispatch ×1

ios ×1

nsdata ×1