Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 357862 - No way to clear singletonCache in InjectorImpl
Summary: No way to clear singletonCache in InjectorImpl
Status: RESOLVED INVALID
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: E4 (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 357460
  Show dependency tree
 
Reported: 2011-09-15 15:05 EDT by Gunnar Wagenknecht CLA
Modified: 2012-12-13 15:00 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gunnar Wagenknecht CLA 2011-09-15 15:05:54 EDT
My IInjector objects have a well-defined life-cycle. However, I noticed that I can't explicitly "dispose" an injector. Is it expected that the Garbage Collector cleans up the singletonCache? If yes I'm wondering if this is easy given the dynamics and the many references (injector, requestor, tracking, etc).

Note, I'm using just the IInjector, not the full EclipseContext.
Comment 1 Gunnar Wagenknecht CLA 2011-09-22 06:55:36 EDT
See also here:
 http://dev.eclipse.org/mhonarc/lists/e4-dev/msg05749.html
Comment 2 Oleg Besedin CLA 2011-09-28 13:59:50 EDT
Yes, the intention is that GC clears singleton cache when injector is not needed anymore.

If we cleared singleton cache how would we be able to ensure that those objects stay singletons? We do need to store them somewhere.
Comment 3 Gunnar Wagenknecht CLA 2011-09-29 01:05:35 EDT
(In reply to comment #2)
> Yes, the intention is that GC clears singleton cache when injector is not
> needed anymore.

How can I remove/dispose listeners that are no longer needed? At some point, I call IInjector#make. This creates an object that may have dynamic injected arguments (updated when they change). When the object is no longer needed, the code no longer references it.

Assuming it will be garbage collected ... how do I then discover that I no longer need the listeners? Is that what IRequestor#isValid is for? If yes then this may lead to a situation where the listeners won't be removed when the system doesn't change and the listeners aren't triggered. I'm wondering if there is a better story for this. Any ideas?

> If we cleared singleton cache how would we be able to ensure that those objects
> stay singletons? We do need to store them somewhere.

Agreed. A long as the injector is alive, it must not re-create singletons. I think what I really want is some way to dispose an injector. Which would also notify the object supplier in order to be able to unregister all listeners hooked into the system for dynamic updates.
Comment 4 Oleg Besedin CLA 2011-09-29 09:39:57 EDT
(In reply to comment #3)
> (In reply to comment #2)
> > Yes, the intention is that GC clears singleton cache when injector is not
> > needed anymore.
> 
> How can I remove/dispose listeners that are no longer needed? At some point, I
> call IInjector#make. This creates an object that may have dynamic injected
> arguments (updated when they change). When the object is no longer needed, the
> code no longer references it.

Non-singleton object references are wrapped into WeakReference and should not prevent objects being garbage collected.

> 
> Assuming it will be garbage collected ... how do I then discover that I no
> longer need the listeners? Is that what IRequestor#isValid is for? If yes then
> this may lead to a situation where the listeners won't be removed when the
> system doesn't change and the listeners aren't triggered. I'm wondering if
> there is a better story for this. Any ideas?

Yes, you are right. If this becomes an issue we could add a "cleanup" task that cleans up lists of WeakReferences every now and then. 

There is also IInjector#uninject() which tells the system to forget about the object.
Comment 5 Gunnar Wagenknecht CLA 2011-09-29 14:24:06 EDT
(In reply to comment #4)
> Yes, you are right. If this becomes an issue we could add a "cleanup" task that
> cleans up lists of WeakReferences every now and then. 
> 
> There is also IInjector#uninject() which tells the system to forget about the
> object.

Well, the issue I have is that my PrimaryObjectSupplier registers listeners in the system in order to update the requestor dynamically. However, there is no way to unregister those listeners or I haven't found it.