简体   繁体   English

如何正确处理UnsafeMutablePointer

[英]How to handle UnsafeMutablePointer correctly

I am a little confused. 我有点困惑。 When do I have to call free and when destroy/dealloc? 什么时候我必须免费拨打电话和/ dealloc? I am working on a short code snippet learning core audio. 我正在研究一个学习核心音频的简短代码片段。 I thought if I call UnsafeMutablePointer<Type>.alloc(size) then I should call destroy & dealloc . 我想如果我调用UnsafeMutablePointer<Type>.alloc(size)那么我应该调用destroydealloc But if I use malloc() or calloc() I am supposed to call free() . 但是如果我使用malloc()calloc()我应该调用free()

In this example from Learning Core Audio the following code snippet makes me wonder: 在这个来自Learning Core Audio的示例中,以下代码片段让我想知道:

var asbds = UnsafeMutablePointer<AudioStreamBasicDescription>.alloc(Int(infoSize))
audioErr = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
                              UInt32(sizeof(fileTypeAndFormat.dynamicType)), &fileTypeAndFormat,
                              &infoSize, asbds)

Here I use alloc . 在这里我使用alloc To free up memory free is called. 调用释放内存是free的。

free(asbds)

But why not 但为什么不呢

asbds.destroy(Int(infoSize))
asbds.dealloc(Int(infoSize))

which I would expect following the rule. 我希望遵循规则。

I would appreciate any help, because this makes my head spin. 我会感激任何帮助,因为这让我头晕目眩。 The documentation says I am responsible for destroying and dealloc so that part is clear, but in which way? 文档说我负责销毁和dealloc,以便部分清晰,但是以哪种方式?

See the UnsafeMutablePointer Structure Reference . 请参阅UnsafeMutablePointer结构参考

The pointer can be in one of the following states: 指针可以处于以下状态之一:

  • Memory is not allocated (for example, pointer is null, or memory has been deallocated previously). 未分配内存(例如,指针为空,或者先前已释放内存)。

  • Memory is allocated, but value has not been initialized. 已分配内存,但尚未初始化值。

  • Memory is allocated and value is initialized. 分配内存并初始化值。

You can use the pointed region safely when "allocated and initialized". “分配和初始化”时,您可以安全地使用指向的区域。 So, if you want to use Swift's UnsafeMutablePointer correctly, you need 2 steps before using and 2 steps after using. 因此,如果要正确使用Swift的UnsafeMutablePointer ,使用前需要2个步骤,使用后需要2个步骤。

(1) Allocate: alloc(_:) . (1)分配: alloc(_:)

(2) Initilize: initialize...() (2) initialize...()initialize...()

You can safely use the allocated and initialized region here. 您可以在此处安全地使用已分配和已初始化的区域。

(3) Deinitialize: destroy(_:) (3)取消初始化: destroy(_:)

(4) Deallocate: dealloc(_:) (4)解除分配: dealloc(_:)


And why you can use free() for alloc(_:) ed memory, that's because Swift uses malloc(_:) in the current implementation of alloc(_:) . 为什么你可以使用free()来获取alloc(_:) ed内存,这是因为Swift在alloc(_:)的当前实现中使用了malloc(_:) alloc(_:) So, using free means that your app depends on the current implementation of the Swift runtime. 因此,使用free意味着您的应用程序依赖于Swift运行时的当前实现。


So, using UnsafeMutablePointer is sort of difficult and annoying. 因此,使用UnsafeMutablePointer有点困难和恼人。 You should consider passing an array as a pointer. 您应该考虑将数组作为指针传递。 In your case, you can write something like this: 在你的情况下,你可以写这样的东西:

    let elementCount = Int(infoSize) / strideof(AudioStreamBasicDescription)
    var asbds: [AudioStreamBasicDescription] = Array(count: elementCount, repeatedValue: AudioStreamBasicDescription())
    audioErr = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
                                  UInt32(sizeof(fileTypeAndFormat.dynamicType)), &fileTypeAndFormat,
                                  &infoSize, &asbds)

(I think you should use this elementCount even when using UnsafeMutablePointer . alloc(_:) or dealloc(_:) uses "number of elements", not "byte size".) (我认为你应该使用这个elementCount即使使用UnsafeMutablePointer alloc(_:)dealloc(_:)使用“number of elements”,而不是“byte size”。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM