dataWithBytesNoCopy和dataWithBytes之间的区别? [英] Difference between dataWithBytesNoCopy and dataWithBytes?

查看:808
本文介绍了dataWithBytesNoCopy和dataWithBytes之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

+(instancetype)dataWithBytes:(const void *)bytes length:(NSUInteger)length; code>





+(instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;



此外,



+(instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;



如果 b ==是,转换为数据后会自动释放字节吗?



我正在研究一个应用程序,几乎完成了。但是最后一个问题是,它在设备上运行时因内存错误而崩溃。它只会在设备上崩溃,而在模拟器中则是完美的。

  malloc:*对象0x17415d0c0错误:无效的指针出队从空闲列表中*在malloc_error_break中设置一个断点以进行调试; 

我已经在这个问题上工作了几天了:
iOS-我的应用崩溃,只有内存错误在设备上运行



但是最终,我发现了这个问题,在我的加密和解密函数中,我有这个问题:

  Byte * buffer =(Byte *)malloc(asciiDataLength); 

用缓冲区处理后,将其转换为 NSData

  NSData * plainData = [NSData dataWithBytesNoCopy:buffer length:asciiDataLength freeWhenDone:YES]; 

此代码导致我的应用连续崩溃,我将其更改为

  NSData * plainData = [NSData dataWithBytes:缓冲区长度:asciiDataLength]; 
free(buffer);

然后我的应用再也不会崩溃。



所以我必须自己释放字节,ARC不会为我释放它。

解决方案

+ dataWithBytes:length:


创建并返回一个数据对象,该数据对象包含从给定缓冲区中复制的给定字节数。


+ dataWithBytesNoCopy:length:



< blockquote>

创建并返回一个数据对象,该对象持有长度为的字节缓冲区 bytes


dataWithBytes 复制数据缓冲区,而 NoCopy 版本则不会。



重要说明:在 dataWithBytesNoCopy:length:

的讨论部分中:


返回的对象拥有字节指针的所有权,并在释放时将其释放。因此, 个字节必须指向分配有 malloc 的内存块。


这意味着使用此方法进行初始化实际上会将内存的所有权移交给 NSData 对象,完成后,它将免费发布 。如果尝试使用未使用 malloc 分配的内存来初始化它,则在释放数据对象时,应用程序将崩溃。



dataWithBytesNoCopy 对于从其他地方获取缓冲区中的字节并准备将其移交给很有用。 NSData 对象,并且不会在该对象之外再次使用它们。



如果您想用自己的内存来初始化数据,使用 + dataWithBytesNoCopy:length:freeWhenDone: 。如果缓冲区将被持久地存储在某个地方,并且不会被更改或释放,这将很有用。



但是,如果您不确定如何正确地手动管理此内存,则可以最好使用 dataWithBytes 。出于性能考虑,提供了其他方法,因为避免复制大块数据可以节省大量时间,但是如果您不确定如何使用它们,则最好不要这样做-不会崩溃的应用程序更可取迅速崩溃的应用。


What is the difference between

+ (instancetype)dataWithBytes:(const void *)bytes length:(NSUInteger)length;

and

+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;

Also,

+ (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b;

if b == YES, will it free the bytes automatically after converted to data?

I am working on an app and almost finished it. But the last problem is it crashes with memory error when it runs on device. It only crashes when on device, but in simulator it is perfect.

"malloc: * error for object 0x17415d0c0: Invalid pointer dequeued from free list * set a breakpoint in malloc_error_break to debug";

I have been working on this issue for several days: iOS - My app crashes with memory Error if only runs on Device

But finally I found the problem, inside my Encryption and Decryption function, I have this:

Byte *buffer    = (Byte*)malloc(asciiDataLength);

After I process with buffer, I convert it to NSData:

NSData *plainData = [NSData dataWithBytesNoCopy:buffer length:asciiDataLength freeWhenDone:YES];

This code caused my app to crash continuously, I changed it to

NSData *plainData = [NSData dataWithBytes:buffer length:asciiDataLength];
free(buffer);

Then my app never crash again.

So, I have to free the Byte by myself, ARC will not free it for me.

解决方案

+ dataWithBytes:length::

Creates and returns a data object containing a given number of bytes copied from a given buffer.

+ dataWithBytesNoCopy:length::

Creates and returns a data object that holds length bytes from the buffer bytes.

dataWithBytes makes a copy of the buffer for the data, while the NoCopy version does not.

Important note: in the discussion section of dataWithBytesNoCopy:length::

The returned object takes ownership of the bytes pointer and frees it on deallocation. Therefore, bytes must point to a memory block allocated with malloc.

This means that initialising with this method essentially hands ownership of the memory to the NSData object, which will release it with free once it is done. If you try to initialise it with memory that you didn't allocate with malloc, your app will crash when the data object is deallocated.

dataWithBytesNoCopy is useful for when you get the bytes in a buffer from somewhere else, and are ready to hand them over to the NSData object, and won't use them yourself again outside of that.

If you want to initialise the data with memory you manage yourself, use + dataWithBytesNoCopy:length:freeWhenDone:. This is useful if the buffer will be stored somewhere persistently, and not changed or released.

However, if you are not sure how to correctly manage this memory manually, it is better to use dataWithBytes. The other methods are present for performance reasons, as avoiding copying large chunks of data can save a lot of time, but if you aren't sure how to use them, it's probably best not to — an app that doesn't crash is preferable to an app that crashes quickly.

这篇关于dataWithBytesNoCopy和dataWithBytes之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆