1

I use CFStreamCreateBoundPair to create a pair of stream in order to upload a large file.The code is like below.

let readStreamPointer = UnsafeMutablePointer<Unmanaged<CFReadStream>?>.alloc(1)
let writeStreamPointer = UnsafeMutablePointer<Unmanaged<CFWriteStream>?>.alloc(1)
let bufferSize = 1024*1024 //1M buffer size
CFStreamCreateBoundPair(kCFAllocatorDefault, readStreamPointer,writeStreamPointer, Int(bufferSize) as CFIndex)
if let rStream = readStreamPointer.memory?.takeRetainedValue(),writeStream = writeStreamPointer.memory?.takeRetainedValue() {

}

Everything goes well except a memory issue.I check out with Instruments and find out CFStreamCreateBoundPair can not free the memory of buffer size which in this case is 1M after both the CFReadStream and CFWriteStream are closed.I have a screen shot too.

Instruments screen shot I am not quite familiar with Core Foundation framework in swift and don't know why it can not be auto released.

Xingxing
  • 580
  • 1
  • 6
  • 17

1 Answers1

0

Here is the way I do it:

class func createBound(inputStream: inout InputStream?, outputStream: inout OutputStream?, bufferSize: UInt) {
    var readStream: Unmanaged<CFReadStream>?
    var writeStream: Unmanaged<CFWriteStream>?
    CFStreamCreateBoundPair(nil, &readStream, &writeStream, CFIndex(bufferSize))
    if inputStream != nil {
        inputStream = readStream!.takeUnretainedValue()
    }
    if outputStream != nil {
        outputStream = writeStream!.takeUnretainedValue()
    }
}
John Difool
  • 5,572
  • 5
  • 45
  • 80