SourcePro : Essential Tools Module User's Guide : Scope Guard Classes : RWTRefHolder<T> : A Note on Pointer and Reference Parameters
A Note on Pointer and Reference Parameters
It is your responsibility to ensure that any parameters that are bound to the scope guard continue to exist at least as long as the scope guard itself. If a parameter bound to the scope guard is destroyed and the scope guard is not dismissed before the end of the local scope, the behavior of the program is undefined. Example:
 
// ...
void deref_print_string(const RWCString* pstr) {
printf ("%s\n", pstr->data());
}
 
// ...
RWCString* pstr = new RWCString("hello world!");
 
RWScopeGuard sg = rwtMakeScopeGuard(deref_print_string, pstr); //1
 
delete pstr; //2
pstr = 0;
 
sg.dismiss(); //3
//1 Binds the scope guard to pstr.
//2 Deallocates pstr. Now the scope guard holds a pointer to an object that no longer exists. If we allow the guard to go out of scope now, it is equivalent to calling
deref_print_string(pstr);
But the pointer pstr now points to a deallocated object. At this point, the only safe solution is to dismiss the guard in order to prevent the call from occurring on exit from the local scope.
//3 Dismisses the guard.
Note that this is not a problem for values, but is important to be aware of when passing a pointer or reference to the rwtMakeScopeGuard() function.