@@ -391,26 +391,28 @@ open class Thread : NSObject {
391
391
return backtraceAddresses { ( addresses, count) in
392
392
var symbols : [ String ] = [ ]
393
393
394
- let buffer : UnsafeMutablePointer < UInt8 > =
395
- . allocate( capacity: MemoryLayout < SYMBOL_INFO > . size + 128 )
396
- defer { buffer. deallocate ( ) }
397
-
398
- let pSymbolInfo =
399
- UnsafeMutableRawPointer ( buffer) . assumingMemoryBound ( to: SYMBOL_INFO . self)
400
- pSymbolInfo. pointee. SizeOfStruct = ULONG ( MemoryLayout< SYMBOL_INFO> . size)
401
- pSymbolInfo. pointee. MaxNameLen = 128
402
-
403
394
let addresses : UnsafeMutableBufferPointer < PVOID ? > =
404
395
UnsafeMutableBufferPointer < PVOID ? > ( start: addresses, count: count)
405
- for address in addresses {
406
- var dwDisplacement : DWORD64 = 0
407
- if SymFromAddr ( hProcess, DWORD64 ( UInt ( bitPattern: address) ) ,
408
- & dwDisplacement, pSymbolInfo) {
409
- withUnsafePointer ( to: pSymbolInfo. pointee. Name) {
410
- symbols. append ( String ( cString: $0) )
396
+ withUnsafeTemporaryAllocation ( byteCount: MemoryLayout < SYMBOL_INFO > . size + 127 ,
397
+ alignment: 8 ) { buffer in
398
+ let pSymbolInfo : UnsafeMutablePointer < SYMBOL_INFO > =
399
+ buffer. baseAddress!. assumingMemoryBound ( to: SYMBOL_INFO . self)
400
+
401
+ for address in addresses {
402
+ pSymbolInfo. pointe. SizeOfStruct =
403
+ ULONG ( MemoryLayout< SYMBOL_INFO> . size)
404
+ pSymbolInfo. pointee. MaxNameLen = 128
405
+
406
+ var dwDisplacement : DWORD64 = 0
407
+ if SymFromAddr ( hProcess, DWORD64 ( UInt ( bitPattern: address) ) ,
408
+ & dwDisplacement, & pSymbolInfo. pointee) {
409
+ symbols. append ( String ( unsafeUninitializedCapacity: Int ( pSymbolInfo. pointee. NameLen) + 1 ) {
410
+ strncpy ( $0. baseAddress, & pSymbolInfo. pointee. Name, $0. count)
411
+ return $0. count
412
+ } )
413
+ } else {
414
+ symbols. append ( " \( address) " )
411
415
}
412
- } else {
413
- symbols. append ( " \( pSymbolInfo. pointee) " )
414
416
}
415
417
}
416
418
0 commit comments