问题描述
我有点困惑.什么时候我必须拨打免费电话,什么时候销毁/取消分配电话?我正在研究一个简短的代码片段,以学习核心音频.我以为如果我呼叫UnsafeMutablePointer<Type>.alloc(size)
,那我应该呼叫destroy
& dealloc
.但是,如果我使用malloc()
或calloc()
,我应该打电话给free()
.
I am a little confused. When do I have to call free and when destroy/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
.But if I use malloc()
or calloc()
I am supposed to call free()
.
在学习核心音频的此示例中,以下代码片段使我感到奇怪:
In this example from Learning Core Audio the following code snippet makes me wonder:
var asbds = UnsafeMutablePointer<AudioStreamBasicDescription>.alloc(Int(infoSize))
audioErr = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
UInt32(sizeof(fileTypeAndFormat.dynamicType)), &fileTypeAndFormat,
&infoSize, asbds)
在这里我使用alloc
.要释放内存,请调用free
.
Here I use alloc
. To free up memory free
is called.
free(asbds)
但是为什么不
asbds.destroy(Int(infoSize))
asbds.dealloc(Int(infoSize))
我希望遵循规则.
我将不胜感激,因为这会使我转头.该文档说,我负责销毁和重新分配,以便使该部分清晰可见,但是以哪种方式呢?
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?
推荐答案
-
未分配内存(例如,指针为空或以前已释放内存).
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.
分配和初始化"后,可以安全地使用指向的区域.因此,如果您想正确使用Swift的UnsafeMutablePointer
,则在使用之前需要2个步骤,在使用之后需要2个步骤.
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.
(1)分配:alloc(_:)
.
(2)初始化:initialize...()
您可以在此处安全地使用分配和初始化的区域.
You can safely use the allocated and initialized region here.
(3)取消初始化:destroy(_:)
(4)解除分配:dealloc(_:)
以及为什么可以对alloc(_:)
ed内存使用free()
,这是因为Swift在alloc(_:)
的当前实现中使用malloc(_:)
.因此,使用免费意味着您的应用程序依赖于Swift运行时的当前实现.
And why you can use free()
for alloc(_:)
ed memory, that's because Swift uses malloc(_:)
in the current implementation of alloc(_:)
. So, using free means that your app depends on the current implementation of the Swift runtime.
因此,使用UnsafeMutablePointer
有点困难和烦人.您应该考虑将数组作为指针传递.就您而言,您可以这样写:
So, using UnsafeMutablePointer
is sort of difficult and annoying. 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)
(我认为即使使用UnsafeMutablePointer
,您也应该使用此elementCount
.alloc(_:)
或dealloc(_:)
使用的是元素数量",而不是字节大小".)
(I think you should use this elementCount
even when using UnsafeMutablePointer
. alloc(_:)
or dealloc(_:)
uses "number of elements", not "byte size".)
这篇关于如何正确处理UnsafeMutablePointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!