如何正确处理UnsafeMutablePointer [英] How to handle UnsafeMutablePointer correctly
问题描述
我有点困惑.什么时候我必须拨打免费电话,什么时候销毁/取消分配电话?我正在研究一个简短的代码片段,以学习核心音频.我以为如果我呼叫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?