Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift vs Objective C pointer manipulation issue

Tags:

I have this code in Objective C which works fine:

list = controller->audioBufferList;
list->mBuffers[0].mDataByteSize = inNumberFrames*kSampleWordSize;
list->mBuffers[1].mDataByteSize = inNumberFrames*kSampleWordSize;

And it works fantastic, it updates mDataByteSize field of mBuffers[0] & mBuffers[1]. I tried translating the same in Swift but it doesn't work:

public var audioBufferList:UnsafeMutableAudioBufferListPointer

In function,

let listPtr = controller.audioBufferList.unsafeMutablePointer

let buffers = UnsafeBufferPointer<AudioBuffer>(start: &listPtr.pointee.mBuffers, count: Int(listPtr.pointee.mNumberBuffers))

for var buf in buffers {
    buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
    NSLog("Data byte size \(buf.mDataByteSize)")
}

for buf in buffers {
    NSLog("Data byte size \(buf.mDataByteSize)")
}

The mDataByteSize is not updated. The NSLog on reading back in second for loop points to original values, not updated ones. It seems var buf is referring to another buf by making a copy. How do I fix it? It's a pure Swift language issue that I am not able to understand.

EDIT: As pointed out by Martin, I fixed the issue by modifying the for loop as

 for i in 0..<Int(listPtr.pointee.mNumberBuffers) {
    buffers[i].mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
}

Now it works. But that has further aroused my curiosity in Swift Language, how non intuitive it is and how irritating it is for developers who use pointers to manipulate stuff. Why do the following loops fail? Are the var buffers copy by value?

     for buf in buffers {
         var buffer = buf
         buffer.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
     }

Or

    for var buf in buffers {
        buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
    }

EDIT 2: Hamish's answer raises doubts about validity of using listPtr anywhere. I was using listPtr in a number of calls, such as:

let status = AudioUnitRender(controller.audioUnit!, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, listPtr)

I now need to know where can we use listPtr and where we can not!

like image 820
Deepak Sharma Avatar asked Nov 27 '18 20:11

Deepak Sharma


People also ask

What do you think are the key differences between Objective-C and swift?

Swift is a general-purpose, high-level programming language which is highly concerned about safety, performance. Objective C is an general purpose language which is considered as superset of C language it was designed in an aim of providing object-oriented capabilities.

Is Swift faster than Objective-C?

Apple's official website claims that Swift is up to 2.6 times faster than Objective-C. They named the language “Swift” for a reason. Swift's simpler syntax and compile-time type checking help increase its performance. But the most significant boost in performance comes from its memory management and scalability.

How is the difference between swift and Objective-C in terms of collections?

Swift supports Dictionaries, Functions, Closures, Enumerations, Structures, etc., whereas Objective C supports Posing, Extensions, Dynamic Binding, Protocol, Composite Objects, Memory Management, and Enumerations.

Is Objective-C Memory Safe?

What's important to understand about Objective-C' safety is that it uses null pointers. The pointer is the component of C++ and other C-based languages and it can cause vulnerabilities in security. It's the method for exposing values that gives developers higher access to the data.


1 Answers

For the call:

let buffers = UnsafeBufferPointer<AudioBuffer>(start: &listPtr.pointee.mBuffers, count: Int(listPtr.pointee.mNumberBuffers))

&listPtr.pointee.mBuffers produces a temporary pointer valid only for the duration of the call to UnsafeBufferPointer's initialiser. Therefore attempting to use the buffer pointer results in undefined behaviour (the compiler will hopefully warn on such cases in Swift 5.1).

Instead, you can iterate directly over UnsafeMutableAudioBufferListPointer, as it conforms to MutableCollection.

For example:

for index in audioBufferList.indices {
  audioBufferList[index].mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
  print("Data byte size \(audioBufferList[index].mDataByteSize)")
}

for buffer in audioBufferList {
  print("Data byte size \(buffer.mDataByteSize)")
}
like image 164
Hamish Avatar answered Oct 03 '22 23:10

Hamish