1 #ifndef _WDL_DESTROYCHECK_H_
2 #define _WDL_DESTROYCHECK_H_
4 // this is a useful class for verifying that an object (usually "this") hasn't been destroyed:
5 // to use it you add a WDL_DestroyState as a member of your class, then use the WDL_DestroyCheck
6 // helper class (creating it when the pointer is known valid, and checking it later to see if it
11 // WDL_DestroyState dest;
15 // calling code (on myClass *classInstnace):
16 // WDL_DestroyCheck chk(&classInstance->dest);
18 // if (!chk.isOK()) printf("classInstance got deleted!\n");
20 // NOTE: only use this when these objects will be accessed from the same thread -- it will fail miserably
21 // in a multithreaded environment
26 class WDL_DestroyCheck
29 class WDL_DestroyStateNextRec
{ public: WDL_DestroyCheck
*next
; };
31 WDL_DestroyStateNextRec n
, *prev
;
32 WDL_DestroyCheck(WDL_DestroyStateNextRec
*state
)
37 if ((n
.next
=prev
->next
)) n
.next
->prev
= &n
;
46 if (n
.next
) n
.next
->prev
= prev
;
50 bool isOK() { return !!prev
; }
53 class WDL_DestroyState
: public WDL_DestroyCheck::WDL_DestroyStateNextRec
56 WDL_DestroyState() { next
=NULL
; }
60 WDL_DestroyCheck
*p
= next
;
61 while (p
) { WDL_DestroyCheck
*np
= p
->n
.next
; p
->prev
=NULL
; p
->n
.next
=NULL
; p
=np
; }