Swift中的CMBlockBuffer,UnsafeMutablePointer等

时间:2016-02-29 01:13:16

标签: pointers swift2 xcode7.2

我正在尝试转换Apple的代码示例中提供的一些Objective C代码:https://developer.apple.com/library/mac/samplecode/avsubtitleswriterOSX/Listings/avsubtitleswriter_SubtitlesTextReader_m.html

到目前为止我得出的结果如下:

func copySampleBuffer() -> CMSampleBuffer? {

    var textLength : Int = 0
    var sampleSize : Int = 0

    if (text != nil) {
        textLength = text!.characters.count
        sampleSize = text!.lengthOfBytesUsingEncoding(NSUTF16StringEncoding)
    }

    var sampleData = [UInt8]()

    // Append text length
    sampleData.append(UInt16(textLength).hiByte())
    sampleData.append(UInt16(textLength).loByte())

    // Append the text
    for char in (text?.utf16)! {
        sampleData.append(char.bigEndian.hiByte())
        sampleData.append(char.bigEndian.loByte())
    }

    if (self.forced) {
        // TODO
    }

    let samplePtr = UnsafeMutablePointer<[UInt8]>.alloc(1)
    samplePtr.memory = sampleData

    var sampleTiming = CMSampleTimingInfo()
    sampleTiming.duration = self.timeRange.duration;
    sampleTiming.presentationTimeStamp = self.timeRange.start;
    sampleTiming.decodeTimeStamp = kCMTimeInvalid;

    let formatDescription = copyFormatDescription()

    let dataBufferUMP = UnsafeMutablePointer<Optional<CMBlockBuffer>>.alloc(1)

    CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, samplePtr, sampleSize, kCFAllocatorMalloc, nil, 0, sampleSize, 0, dataBufferUMP);

    let sampleBufferUMP = UnsafeMutablePointer<Optional<CMSampleBuffer>>.alloc(1)

    CMSampleBufferCreate(kCFAllocatorDefault, dataBufferUMP.memory, true, nil, nil, formatDescription, 1, 1, &sampleTiming, 1, &sampleSize, sampleBufferUMP);

    let sampleBuffer = sampleBufferUMP.memory

    sampleBufferUMP.destroy()
    sampleBufferUMP.dealloc(1)

    dataBufferUMP.destroy()
    dataBufferUMP.dealloc(1)

    samplePtr.destroy()
    //Crash if I call dealloc here
    //Error is: error for object 0x10071e400: pointer being freed was not allocated
    //samplePtr.dealloc(1)

    return sampleBuffer;
}

我想避免&#34;不安全*&#34;在可能的情况下,尽管我不确定这是否可行。我还看了一下使用结构然后以某种方式看到打包它,但我看到的例子似乎是基于sizeof,它使用定义的大小,而不是结构的当前大小。这本来就是我用过的结构:

struct SubtitleAtom {
    var length : UInt16
    var text : [UInt16]
    var forced : Bool?
}

对于此功能的最合适的Swift 2代码的任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

所以,首先,你编码使用这种模式

class C { deinit { print("I got deinit'd!") } }
struct S { var objectRef:AnyObject? }

func foo() {
    let ptr = UnsafeMutablePointer<S>.alloc(1)
    let o = C()
    let fancy = S(objectRef: o)
    ptr.memory = fancy
    ptr.destroy() //deinit runs here!
    ptr.dealloc(1) //don't leak memory
}
// soon or later this code should crash :-)
(1..<1000).forEach{ i in
    foo()
    print(i)
}

在操场上试试,很可能会崩溃:-)。它有什么问题?问题在于不平衡的保留/释放周期。如何以安全的方式写出相同的内容?你删除了dealloc部分。但尝试在我的代码片段中执行此操作并查看结果。代码再次崩溃:-)。唯一安全的方法是正确初始化和去初始化(破坏)底层ptr的内存,如下一个片段所示

class C { deinit { print("I got deinit'd!") } }
struct S { var objectRef:AnyObject? }

func foo() {
    let ptr = UnsafeMutablePointer<S>.alloc(1)
    let o = C()
    let fancy = S(objectRef: o)
    ptr.initialize(fancy)
    ptr.destroy()
    ptr.dealloc(1)
}

(1..<1000).forEach{ i in
    foo()
    print(i)
}

现在代码按预期执行,所有保留/释放周期均衡