Handle events before destroying objects

Register event handlers with the object manager, and fire the events
from within DiscardObjects before discarding any objects.

This mitigates problems where event handlers fired in global destruction,
after the object manager already had been garbage-collected
Hi Martin,

On 30.06.2014 09:52, Martin Gruner wrote:
> In Kernel/System/ObjectManager.pm:
> 
>> +    for my $Counter (1..10) {
>> +        $HasQueuedTransactions = 0;
>> +        for my $EventHandler ( @{ $Self->{EventHandlers} } ) {
>> +            if ($EventHandler->EventHandlerHasQueuedTransactions) {
>> +                $HasQueuedTransactions = 1;
>> +                $EventHandler->EventHandlerTransaction();
>> +            }
>> +        }
>> +        if ( !$HasQueuedTransactions ) {
>> +            last EVENTS;
>> +        }
>> +    }
>> +    if ( $HasQueuedTransactions ) {
>> +        warn "Unable to handle all pending events in 10 iterations";
>> +    }
>> +    delete $Self->{EventHandlers};
> 
> Sorry @moritz <https://github.com/moritz>, I still wonder if there can
> be side effets by the references we have here. What if an even
> triggering object is also managed by the OM? If it is supposed to be
> discarded/renewed somewhere before the destruction, the reference here
> would prevent it from being destroyed, no?
> Should we use weak references?

You are right here again. I only thought of objects under the control of
the object manager; for those that aren't, it's wrong to keep strong
references.

Commit c119dfcf5f803d7cc7c0d3956285cdd34fc6056a makes them weak
references (and 9fd2e fixes the coding style).

Cheers,
Moritz