Close or ESC key
© QIOS

An example illustrating the problem

Imagine a class that has an object that publishes events and an object that consumes the events. If you want the the publisher to remain and clean up the consumer. Just by removing the references to the publisher the consumer will never be collected, since via the eventhandler the publisher still has a reference to the consumer. We have to remove the eventhandler manually if we want the consumer to be collected by the garbage collector.

Show Code Snippet - Handling default events

 

An example illustrating the transparency of the solution

It can become a time-consuming process to use a memory profiler to check which objects in your application aren't being collected and which reference is the cause. This is why we have developed a QWeakDelegate. By merely changing the event of the MyPublisher class the eventhandler will become a WeakReference and the consumer will be freed normally. The following example uses a QWeakDelegate to publish events and it shows that no changes are neccessary for consuming events.

Show Code Snippet - Handling QWeakEvents

 

How it works

We see now in the example above that for the consumer nothing actually changes. The change is completely transparent to the consumer. Of course, the publisher needs some code changes, but it's not a drastic change. The code snippet below shows the way a normal event is published:

Show Code Snippet - Publishing a default event

 

To change the event structure so that any consumers can be collected normally the following changes are neccesary:

Show Code Snippet - Publishing a QWeakEvent

 

Consuming events

The publisher in the code snippet above publishes weak events, but you don't have control over all events. For instance, you can't change the way a button publishes the Click event. For these situations we have created the QWeakEventConsumer class. The code snippet below shows the MyConsumer class from the examples above.

Show Code Snippet - Consuming default events

 

Both the consumer and publishers use the default way of consuming and publishing an event. To use a QWeakConsumer we change the code to the following:

Show Code Snippet - Consuming default events via a QWeakConsumer