EventAggregator with dynamic subscribe/unsubscribe

Issue #18 new
Former user created an issue

I use Eventaggregator extensively.

I have 2 classes with different Handle-procedures. If I unsubscribe class 2 in a Handle-procedure of class 1, I get access violations. This happens in a deep nested object hierarchie, so I find no other solution or workaround.

I attachted a very simple sample program.

Is it possible to inform the handle-loop about a change in the handle-list?

Comments (4)

  1. Stefan Glienke repo owner

    You are using something from a feature branch that is work in progress at this point. Thus I am unable to give you full support.

    However the test runs without problems - what exactly should happen?

  2. Former user Account Deleted

    I use XE5 with FastMM4 with CheckHeapForCorruption and FullDebugMode. Sometimes it seams to be correct sometimes I get av's.

    In "TEventAggregator.Publish...Marshal(" you iterate over LToNotify. This is an Array from FHandlers. If I Unsubscribe during this loop, a handler would destroyed and the pointer in the array looks to "nowhere". The problem is, that we have only one list for all types of messages. So if I send message type 1 and one handler create/freed objects responsible to message type 2, the list will be changed and your loop is incorrect.

  3. Stefan Glienke repo owner

    Adding

    if not FHandlers.Contains(LHandler) then Continue;
    

    into the loop should prevent the problem.

    But be aware that this might raise some other problems with concurrency as it's just a hotfix. You might wrap it into a TMonitor block.

  4. Former user Account Deleted

    Argghh. Clear. Simple but effective solution. I don't have seen that. Thank you.

  5. Log in to comment