@@ -6330,40 +6330,47 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6330
6330
/* STATIC_METHODS_END */
6331
6331
6332
6332
static var nativelyExposedInstances = [ UInt: NativeTraitWrapper] ( )
6333
- static var nativelyExposedInstanceReferenceCounter = [ UInt: UInt ] ( )
6333
+ static var nativelyExposedInstanceReferenceCounter = [ UInt: Int ] ( )
6334
6334
6335
6335
public class func cacheInstance( instance: NativeTraitWrapper ) {
6336
6336
let key = instance. globalInstanceNumber
6337
- let counter = Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0
6338
- Self . nativelyExposedInstanceReferenceCounter [ key] = counter + 1
6339
- if counter == 0 {
6340
- print ( " Caching global instance \( key) " )
6337
+ let referenceCount = ( Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0 ) + 1
6338
+ Self . nativelyExposedInstanceReferenceCounter [ key] = referenceCount
6339
+ if referenceCount == 1 {
6340
+ print ( " Caching global instance \( key) . Cached instance count: \( nativelyExposedInstanceReferenceCounter . count ) " )
6341
6341
Self . nativelyExposedInstances [ key] = instance
6342
6342
}
6343
6343
}
6344
6344
6345
6345
public class func instanceToPointer( instance: NativeTraitWrapper ) -> UnsafeMutableRawPointer {
6346
- let pointer = UnsafeMutableRawPointer ( bitPattern: instance. globalInstanceNumber) !
6346
+ let key = instance. globalInstanceNumber
6347
+ let pointer = UnsafeMutableRawPointer ( bitPattern: key) !
6347
6348
// don't automatically cache the trait instance
6348
6349
// Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
6349
6350
return pointer
6350
6351
}
6351
6352
6352
6353
public class func pointerToInstance< T: NativeTraitWrapper > ( pointer: UnsafeRawPointer , sourceMarker: String ? ) -> T {
6353
6354
let key = UInt ( bitPattern: pointer)
6354
- print ( " \( sourceMarker) > Releasing global instance \( key) " , severity: . DEBUG)
6355
6355
let value = Self . nativelyExposedInstances [ key] as! T
6356
+ let referenceCount = Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0
6357
+ if referenceCount < 1 {
6358
+ print ( " Bad lookup: non-positive reference count for instance \( key) : \( referenceCount) ! " , severity: . ERROR)
6359
+ }
6356
6360
return value
6357
6361
}
6358
6362
6359
6363
public class func removeInstancePointer( instance: NativeTraitWrapper ) -> Bool {
6360
6364
let key = instance. globalInstanceNumber
6361
- Self . nativelyExposedInstanceReferenceCounter [ key ] = Self . nativelyExposedInstanceReferenceCounter [ key] ! - 1
6362
- let referenceCount = Self . nativelyExposedInstanceReferenceCounter [ key]
6365
+ let referenceCount = ( Self . nativelyExposedInstanceReferenceCounter [ key] ?? 0 ) - 1
6366
+ Self . nativelyExposedInstanceReferenceCounter [ key] = referenceCount
6363
6367
if referenceCount == 0 {
6364
6368
print ( " Uncaching global instance \( key) " )
6369
+ // TODO: fix counting
6365
6370
Self . nativelyExposedInstances. removeValue ( forKey: key)
6366
6371
instance. pointerDebugDescription = nil
6372
+ } else if referenceCount < 0 {
6373
+ print ( " Bad uncache: negative reference count ( \( referenceCount) ) for instance \( key) ! " , severity: . ERROR)
6367
6374
}
6368
6375
return true
6369
6376
}
@@ -6512,7 +6519,7 @@ withUnsafePointer(to: Bindings.array_to_tuple32(array: random_seed_bytes)) { (ra
6512
6519
*/
6513
6520
6514
6521
public class func get_ldk_swift_bindings_version( ) -> String {
6515
- return " be7ee7abb0bf358457f0e91a0403146885e9bc24 "
6522
+ return " eb40db30cfc9a35a8947dbbc17d7fdcc05bc3b50 "
6516
6523
}
6517
6524
6518
6525
}
0 commit comments