Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Initialization of 'UnsafeBufferPointer<T>' results in a dangling buffer pointer

After update to Swift 5.2 / Xcode 11.4 got a warning to following code:

extension Data {      init<T>(from value: T) {         var value = value         let pointer = UnsafeBufferPointer(start: &value, count: 1)         self.init(buffer: pointer)     }      func to<T>(type: T.Type) -> T {         return self.withUnsafeBytes { $0.load(as: T.self) }     } } 

On line let pointer = UnsafeBufferPointer(start: &value, count: 1) I got

Initialization of 'UnsafeBufferPointer' results in a dangling buffer pointer

I can use @silenceWarning but it's dirty solution. Maybe I need to store pointer somewhere and clean it in the future?

like image 779
Exey Panteleev Avatar asked Mar 25 '20 21:03

Exey Panteleev


People also ask

Does unsafebufferpointer produce a dangling pointer?

This still generates the warning that UnsafeBufferPointer is producing a dangling Pointer but the hints say "produces a pointer valid only for the duration of the call to 'init(start:count:)'" But the return from UnsafeBufferPointer isn't assigned to anything, so I couldn't use it outside the scope of the init if I tried.

What does initialization of'unsafebufferpointer'result in?

Initialization of 'UnsafeBufferPointer' results in a dangling buffer pointer I can use @silenceWarning but it's dirty solution. Maybe I need to store pointer somewhere and clean it in the future? swiftunsafe-pointersswift5.2xcode11.4 Share Improve this question Follow asked Mar 25 '20 at 21:38 Exey PanteleevExey Panteleev

What does'unsafebufferpointer<T>'ERROR result in?

Warning: Initialization of 'UnsafeBufferPointer<T>' results in a dangling buffer pointer Ask Question Asked1 year, 8 months ago Active4 months ago Viewed8k times 33 3 After update to Swift 5.2 / Xcode 11.4 got a warning to following code:

What is the result of initialization of'unsafemutablerawpointer'in Swift?

Initialization of 'UnsafeMutableRawPointer' results in a dangling pointer 1 memset with Swift 5.2 1 CMVideoFormatDescriptionCreateFromH264ParameterSets throw Initialization of 'UnsafePointer<Int>' results in a dangling pointer


1 Answers

I also met these annoying warnings.

var str = "aaaaabbbbbccccc" var num1 = 1 var num2 = 22  var data = Data() // Initialization of 'UnsafeBufferPointer<String>' results in a dangling buffer pointer data.append(UnsafeBufferPointer(start: &str, count: 1))  // Initialization of 'UnsafeBufferPointer<Int>' results in a dangling buffer pointer data.append(UnsafeBufferPointer(start: &num1, count: 1)) // Initialization of 'UnsafeBufferPointer<Int>' results in a dangling buffer pointer  data.append(UnsafeBufferPointer(start: &num2, count: 1))  

Considering @greg's answer, I put the Data.append into withUnsafePointer's closure, and it does not show warnings anymore.

withUnsafePointer(to: &str) { data.append(UnsafeBufferPointer(start: $0, count: 1)) } // ok withUnsafePointer(to: &num1) { data.append(UnsafeBufferPointer(start: $0, count: 1)) } // ok withUnsafePointer(to: &num2) { data.append(UnsafeBufferPointer(start: $0, count: 1)) } // ok 

Here is the extension

extension Data {     init<T>(value: T) {         self = withUnsafePointer(to: value) { (ptr: UnsafePointer<T>) -> Data in             return Data(buffer: UnsafeBufferPointer(start: ptr, count: 1))         }     }      mutating func append<T>(value: T) {         withUnsafePointer(to: value) { (ptr: UnsafePointer<T>) in             append(UnsafeBufferPointer(start: ptr, count: 1))         }     } } 
like image 102
Chen OT Avatar answered Sep 20 '22 17:09

Chen OT