Re: EventSubject as WeakHashMap key in EventManager

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon Nov 24 2003 - 18:01:17 EST

  • Next message: Andrus Adamchik: "Re: Maven"

    On Nov 24, 2003, at 7:52 AM, Holger Hoffstätte wrote:
    > On Fri, 21 Nov 2003, Andrus Adamchik wrote:
    >
    >> I remember there was a discussion way back about weak references in
    >> the
    >> event mechanism. Now that I am doing lots of testing with it (and btw,
    >> 1.1 EventManager seems to be very robust), I ran a few times into a
    >> problem of deallocated invocation queues due to the fact that
    >> EventSubject used wasn't owned by any object, but instead was created
    >> locally.
    >
    > Can you point me to an example? Is it one of the unit tests?

    The example was somewhat arbitrary. I have a package in my sandbox
    called ...cayenne.examples.hrbot (I use it to test different things
    simultaneously, so it is probably in an incoherent state now). Main
    class of the hrbot package would create a new subject in
    "configureExternalEvents" method. The subject and its queue used to get
    deallocated until I stored a reference to the EventBridge for the
    duration of the app (EventBridge is a listener, but it also has an
    internal reference to subject).

    Further looking at the code I came to the conclusion (untested) that if
    there are no references to the subject anywhere (say it was created and
    used locally in a method to register one or more listeners that have
    application scope), subject and all its listeners will be removed from
    the event manager, even though the listeners themselves are not
    deallocated yet. I don't know if I am wrong here, I guess we need to
    test this assumption.

    I agree that using WeakReferences instead of a cleanup thread is much
    better approach. Unfortunately if the problem above is indeed a
    problem, and not just my imagination, we have to either do a cleanup
    thread (which might be good to remove dead invocations anyways), or
    make it explicit in the docs that users must hold on to their subjects
    (which may scare lost of Java ppl. who never had to deal with reference
    counting, and manually freeing memory :-)).

    Andrus



    This archive was generated by hypermail 2.0.0 : Mon Nov 24 2003 - 18:01:21 EST