On Wed, 27 Aug 2003, Ian Goldby wrote:
This is the technique I usually use. The only thing you have to watch is to make sure that you tidy up properly just before each return. In extreme cases, it's probably better to use goto (especially if undoing a non-existant action is benign - unfortunately with malloc/free it's not):
if (!(ptr = malloc(...))) return; if (CreateMutex(...)) { free(ptr); return; } if (CreateEvent(...)) { free(ptr); DeleteMutex(...); return; } etc.
Right -- in most cases (including malloc/free) initializing variables to a known value (usually 0) solves such problems. If that does not work, you can have a multi-stage exit, and just goto the right stage. But I agree with you such cases are not very common. But for the cases where this makes sense, I'd rather make good use of goto rather than make a complex case even harder to understand just for the benefit of sticking to the don't-use-goto dogma.