Nipun
Nipun

Reputation: 2261

Blocking disposing off object by GC due to callback

I have a C++/CLI application where I expose subscription API for our clients. In this API, they give me a C# callback (Action) which I keep calling from my application to publish some data.

Problem is: clients say when their object is gone out of scope, GC is not called on their object because the callback they have given me is their member variable and my app have a strong reference to it, which is true, I store this callback in my app.

I understand this would be very common problem in this kind of scenario. But I am not sure how this problem can be resolved.
So I need advice:
How my app should handle this callback so that it doesn't block GC to dispose client object.

If more details are required, pls leave a comment.

Upvotes: 1

Views: 56

Answers (1)

nvoigt
nvoigt

Reputation: 77354

You have two options:

  • Use weak references instead of your strong references.

  • Give your users a chance to deregister the Action they registered with you.

Both should work.

Upvotes: 2

Related Questions