Skip to content

How to add a gc callback listener to an object when it's be GCed(free)? #1920

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

Closed
JavanZhu opened this issue Jun 23, 2021 · 4 comments
Closed
Labels

Comments

@JavanZhu
Copy link

Hi all!
My AssemblyScript wasm app call some native API which implemented by wasm runtime,
and some native_api_free() API must be called manually to free some native resource by AssemblyScript wasm
app when there is no need to use it, otherwise it will cause memory leak problem.

export function _start(): void {
  // call native api which implemented by webassembly runtime
  var stringInstance = covertToString(nativeApi());
  // add a listener to listen this object's lifecycle
  stringInstance.addGCListener(gcCallbackMethod(stringInstance));
}

function gcCallbackMethod(inst: string) {
  // when this object is being free, call nativeAPIFree() to free some native resource so that developer not need to free it manually.
  nativeApiFree(inst);
}

For some reason, it's hard to free it manually. Is it possible to add a GC callback listener to a new AssemblyScript object? So when it is free(GCed), we can receive a GC callback message about this object, then call native_api_free() to free some native resources. Thanks.

@MaxGraey
Copy link
Member

MaxGraey commented Jun 23, 2021

It will available via finalizers I guess:. See related thread: #1299
But currently this doesn't support yet.

@jtenner
Copy link
Contributor

jtenner commented Jun 23, 2021

Aspect caches strong results and frees them using rtrace. But that requires Javascript host code.

@JavanZhu
Copy link
Author

It will available via finalizers I guess:. See related thread: #1299
But currently this doesn't support yet.

Thank you very much for your quick reply, this is helpful.

@JavanZhu
Copy link
Author

Hi @jtenner, thank you for your suggestion.

But that requires Javascript host code.

I understand that means we should execute AssemblyScript on runtime like chrome, which we AssemblyScript module(wasm) from javascript? Currently my wasm app(written by AssemblyScript) run on wasm-micro-runtime. not sure I understand correctly, thanks.

@JavanZhu JavanZhu closed this as completed Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants