git » sdk » commit ea88903

Add option to register finalizer for any object

author Stephen Paul Weber
2026-03-04 14:50:01 UTC
committer Stephen Paul Weber
2026-03-04 19:28:26 UTC
parent e3bb7aed44766785c059786fe2f9401161b04da2

Add option to register finalizer for any object

HaxeCBridge.hx +24 -2

diff --git a/HaxeCBridge.hx b/HaxeCBridge.hx
index e25104a..930ddca 100644
--- a/HaxeCBridge.hx
+++ b/HaxeCBridge.hx
@@ -1023,14 +1023,20 @@ class HaxeCBridge {
 			}
 			
 			HAXE_C_BRIDGE_LINKAGE
-			void ${namespace}_release(const void* ptr) {
+			void ${namespace}_release(const void *ptr) {
 				struct Callback {
-					static void run(void* data) {
+					static void run(void *data) {
 						HaxeCBridge::releaseHaxePtr(data);
 					}
 				};
 				HaxeCBridgeInternal::runInMainThread(Callback::run, (void*)ptr);
 			}
+
+			HAXE_C_BRIDGE_LINKAGE
+			void ${namespace}_set_finalizer(const void *ptr, void (*finalize)(void *)) {
+				// This is already locked internally so should be safe to just call
+				__hxcpp_set_finalizer((hx::Object*)ptr, (void*)finalize);
+			}
 		')
 		+ ctx.functionDeclarations.map(d -> generateFunctionImplementation(namespace, d)).join('\n') + '\n'
 		;
@@ -1883,6 +1889,22 @@ class CConverterContext {
 				})
 			});
 			supportDeclaredFunctionIdentifiers.set(functionIdent, Context.currentPos());
+
+			supportFunctionDeclarations.push({
+				doc: code('
+					Register a finalizer to run when this object is garbage collected
+
+					Thread-safety: can be called on any thread.
+
+					@param ptr a handle to an arbitrary SDK object returned from an SDK function
+					@param finalize a function pointer that will be called with ptr right before it is fully released'),
+				kind: Function({
+					name: '${declarationPrefix}_set_finalizer',
+					args: [{name: 'ptr', type: Ident("const void*")}, {name: 'finalize', type: FunctionPointer('finalize', ["ptr"], [Ident("void*")], Ident("void"), [])}],
+					ret: Ident('void')
+				})
+			});
+			supportDeclaredFunctionIdentifiers.set(functionIdent, Context.currentPos());
 		}
 
 		return Ident(typeIdent);