Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not retain objects #622

Open
migueldeicaza opened this issue Dec 2, 2024 · 4 comments
Open

Do not retain objects #622

migueldeicaza opened this issue Dec 2, 2024 · 4 comments

Comments

@migueldeicaza
Copy link
Owner

Using this patch to not retain objects in our internal tables:

diff --git a/Sources/SwiftGodot/Core/Wrapped.swift b/Sources/SwiftGodot/Core/Wrapped.swift
index f440503..bbc7cec 100644
--- a/Sources/SwiftGodot/Core/Wrapped.swift
+++ b/Sources/SwiftGodot/Core/Wrapped.swift
@@ -303,9 +303,9 @@ func bindGodotInstance(instance: some Wrapped, handle: UnsafeRawPointer) {
     tableLock.withLockVoid {
         if frameworkType {
             // Speed optimization, I do not think that we need this
-            //liveFrameworkObjects [handle] = instance
+            liveFrameworkObjects [handle] = WeakWrap(wrapped: instance)
         } else {
-            liveSubtypedObjects [handle] = instance
+            liveSubtypedObjects [handle] = WeakWrap(wrapped: instance)
         }
     }
     
@@ -383,10 +383,17 @@ public func unregister<T:Wrapped> (type: T.Type) {
     }
 }
 
+class WeakWrap {
+    weak var wrapped: Wrapped?
+    public init(wrapped: Wrapped) {
+        self.wrapped = wrapped
+    }
+}
+
 /// Currently contains all instantiated objects, but might want to separate those
 /// (or find a way of easily telling appart) framework objects from user subtypes
-var liveFrameworkObjects: [UnsafeRawPointer:Wrapped] = [:]
-var liveSubtypedObjects: [UnsafeRawPointer:Wrapped] = [:]
+var liveFrameworkObjects: [UnsafeRawPointer:WeakWrap] = [:]
+var liveSubtypedObjects: [UnsafeRawPointer:WeakWrap] = [:]
 
 // Lock for accessing the above
 var tableLock = NIOLock()
@@ -407,7 +414,7 @@ fileprivate var bindingObject: UnsafeMutableRawPointer? = nil
 /// are the only ones that would keep state
 func lookupLiveObject (handleAddress: UnsafeRawPointer) -> Wrapped? {
     tableLock.withLock {
-        return liveSubtypedObjects [handleAddress]
+        return liveSubtypedObjects [handleAddress]?.wrapped
     }
 }
 
@@ -419,14 +426,14 @@ func lookupLiveObject (handleAddress: UnsafeRawPointer) -> Wrapped? {
 /// we do not get the base type, but the most derived one
 func lookupFrameworkObject (handleAddress: UnsafeRawPointer) -> Wrapped? {
     tableLock.withLock {
-        return liveFrameworkObjects [handleAddress]
+        liveFrameworkObjects [handleAddress]?.wrapped
     }
 }
 
 func objectFromHandle (nativeHandle: UnsafeRawPointer) -> Wrapped? {
     tableLock.withLock {
         if let o = (liveFrameworkObjects [nativeHandle] ?? liveSubtypedObjects [nativeHandle]) {
-            return o
+            return o.wrapped
         }
         
         return nil

@migueldeicaza
Copy link
Owner Author

@migueldeicaza
Copy link
Owner Author

@migueldeicaza
Copy link
Owner Author

@migueldeicaza
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant