The following code reads data from a file and populates the properties of the provided FileHeader struct. The code works, but every time I initialize the 'pointer' with a UnsafeMutableRawBufferPointer I get the'dangling buffer pointer' warning. I assume I need to use this within a 'withUnsafeBytes' closure somehow, but I can't seem to get the syntax right, despite a day of searching the internet for examples and fiddling with the bits. My head hurts.
Any suggestions on how to modify this code to eliminate the warnings?
Many thanks in advance.
private func loadFileHeader( fileHandle: FileHandle, fileHeader: inout FileHeader ) {
var pointer: UnsafeMutableRawBufferPointer
pointer = UnsafeMutableRawBufferPointer(start: &fileHeader.fileCode, count: 4) // *
fileHandle.readData(ofLength: 4).copyBytes(to: pointer)
fileHeader.fileCode = fileHeader.fileCode.bigEndian
SeekTo(fileHandle: fileHandle, offset: 24)
pointer = UnsafeMutableRawBufferPointer(start: &fileHeader.fileLength, count: 4) // *
fileHandle.readData(ofLength: 4).copyBytes(to: pointer)
fileHeader.fileLength = fileHeader.fileLength.bigEndian
// etc...
}
// * ==> Initialization of 'UnsafeMutableRawBufferPointer' results in a dangling buffer pointer
Any suggestions on how to modify this code to eliminate the warnings?
Many thanks in advance.
private func loadFileHeader( fileHandle: FileHandle, fileHeader: inout FileHeader ) {
var pointer: UnsafeMutableRawBufferPointer
pointer = UnsafeMutableRawBufferPointer(start: &fileHeader.fileCode, count: 4) // *
fileHandle.readData(ofLength: 4).copyBytes(to: pointer)
fileHeader.fileCode = fileHeader.fileCode.bigEndian
SeekTo(fileHandle: fileHandle, offset: 24)
pointer = UnsafeMutableRawBufferPointer(start: &fileHeader.fileLength, count: 4) // *
fileHandle.readData(ofLength: 4).copyBytes(to: pointer)
fileHeader.fileLength = fileHeader.fileLength.bigEndian
// etc...
}
// * ==> Initialization of 'UnsafeMutableRawBufferPointer' results in a dangling buffer pointer