|
|
|
|
|
by btrask
2030 days ago
|
|
Here's a trick that will actually help produce more secure and reliable programs. *outArg = myPtr; myPtr = NULL;
free(aPtr); aPtr = NULL;
Set your pointers to null when you free them! Set them to null when you transfer ownership! Stop leaving dangling pointers everywhere!Some people say they like dangling pointers because they want their program to crash if something is freed when they don't expect it to be. Good! Do this: assert(ptr);
There are also many more tricks you can do once you start nulling pointers. You can use const to mark pointers that you don't own and thus can't free. You can check that buffers are all zero before you free them to catch memory leaks (this requires zeroing out other fields too of course).Please, null out your pointers and stop writing (most) use-after-free bugs! |
|
#define ZFREE(p) do { free(p); p = NULL; } while(0)