spinettaro / delphi-event-bus

Delphi Event Bus (for short DEB) is an Event Bus framework for Delphi
Apache License 2.0
466 stars 110 forks source link

Exception error while using Spring4D Dependency injection and multiple UnRegister. #15

Closed epachsoft closed 5 years ago

epachsoft commented 6 years ago

Hi,

I'm using Delphi Tokyo, latest version. I'm using Spring4D container and declaring an IEventBus (not using the built in singleton) as a singleton.

In one part of my code I call twice the method to UnRegister the current object.

E.g.

if not Assigned(fEventBus) then Exit; // shouldnt be needed but, hey just in case

If fEventBus.IsRegistered(Self) then fEventBus.UnRegister(Self);

Calling this twice will cause an exception. The exception happens while attempting to Acquire the critical section via FCS. It seems that the finalization section was already called and the FCS critical section is now nil, causing the issue.

In general Finalization sections mixed with Dependency containers will cause trouble. I will suggest an implementation that completely skips the use of that methodology if the developer is not using the built in singleton.

Thank you for your great work with this, it is extremely useful. It will be great if you wrap it into a component so its easier to share and it can be exposed via Delphinus or GetIt.

UPDATE: I did a quick test changing the Critical sections with a TMonitor (new versions are faster than TCriticalSections due to use of Spinlocks) and it did the trick. Another option could be to put the critical section inside of the object and only use the global one when creating the GetDefault singleton.

spinettaro commented 6 years ago

Hi @epachsoft, could you give me a small project that reproduces the issue?

spinettaro commented 5 years ago

@epachsoft is this issue still valid or resolved?

edwinyzh commented 5 years ago

This PR addresses an issue related to unregistering a subscriber.