2

I'm building a Dart app which contains a variety of class objects. The particular class object I'm dealing with contains a variety of stream event listeners on DOM elements. When I remove these objects from the DOM and untrack the class object these listeners persist.

I know that Dart runs garbage collection eventually, but I'm not even 100% sure it will come along and delete these class objects since there is a Watcher and Stream listeners that continue.

My question is, is there a way to actively delete a class object immediately? I tried setting the class object to null but that doesn't seem to work for some reason. When I check if the object exists afterward with a print statement, it still lists it as an instance of that class object.

Furthermore, for what I'm trying to accomplish, canceling streams doesn't seem to be enough. I need to destroy the class object.

1 Answers1

2

Setting references to null is all you can do. Your test seems very weird. How can you print the object if you don't have a reference? If you still have a reference, how can you expect the instance to be collected.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    if an instance has only one reference, but it owns streams to which there are listeners subscribed, is it necessary to cancel the subscriptions before assigning null to the instance reference, or that assignment will kill the whole thing? – nbloqs Apr 12 '19 at 15:33
  • Closing the StreamController should do. It's always a good idea to verify in Observatory yourself. – Günter Zöchbauer Apr 12 '19 at 15:45