On 10/24/2026 3:32 PM, Lawrence D’Oliveiro wrote:
> I had a quick look at that. It seems to have to do with the reality of > graphics hardware implementing different kinds of memory with > different kinds of characteristics, and the impossibility of > completely such ignoring such issues in real-time 3D rendering. > > I expect higher-level APIs (e.g. game engines) will wrap all this > behind their own higher-level abstractions, so regular > programmers/game creators don’t have to worry about it. > > In short, it’s an abstraction that leaks that you accept only because > you have to, not because it’s a good idea for abstractions in general. > If you can successfully hide issues of memory and other resource > allocation behind your abstraction, you would still rather do it than > not. > > So, to get back to my point, the technique I use is based on a form of > structured programming called a “Nassi-Shneiderman diagram”. The > overall structure looks like > > ... initialize object pointer to NULL ... > do /*once*/ > { > ... possible other stuff ... > ... allocate object pointer ... > if (error occurred) > break; > ... possible other stuff using object pointer ... > } > while (false); > ... call free(3) (or equivalent) on object pointer ... > > So you can see there is no way out of the inner block without passing > through the dispose call. Using gotos would contribute nothing except > make it harder to assure yourself of the simplicity of the control > flow. > > Looks deceptively simple. But the same technique can scale up to quite > complex cases.
The graphics case is where the leak is unavoidable, but that’s not why I’d want it. A lib that never allocates behind your back is what you want in embedded, kernel, and arena code, and a foo_create wrapper on top gives you the hidden-allocation version for everyone else. Your do-once cleanup idiom works fine with it. You just call deinit and reclaim the block the way you made it. It’s a way to structure the caller’s error handling, and it doesn’t say anything about who owns the memory. Say memory on a threads stack, no free needed.