There is an object that, before its destruction, must wait for some asynchronous event. How to delay this destruction so that class members continue to exist even after Set foo = Nothing ?

Example: Let it be a player using the Waveform Audio API. Before freeing the buffers, he must wait for the MM_WOM_DONE window message.

Explicit call of some function before linking is unacceptable - I want to avoid resource leakage due to the user’s forgetfulness of the class.

    1 answer 1

    An object can delay its destruction if it maintains a reference to itself within Class_Teminate :

     Option Explicit ' Замените ThisClassName на имя этого класса Private m_objPostponingReference As ThisClassName Private Sub Class_Teminate() Set m_objPostponingReference = Me ' Всё, объект начал существовать сам по себе. После завершения очистки необходимо ' выполнить Set m_objPostponingReference = Nothing, и объект будет окончательно удалён. End Sub 

    This technique works because after returning from Class_Teminate runtime re-checks the reference count and cancels the deletion if it is no longer null.