I do apologize in advance if I am missing something glaringly obvious.
I have two versions of the same program. One intentionally leaks and one does not:
#include <iostream>int main() { int* y = new int; *y = 32; printf("Y is: %i", *y); delete y; //y doesn't leak return 0;}
#include <iostream>int main() { int* y = new int; *y = 32; printf("Y is: %i", *y); //delete y; y leaks return 0;}
Please see:https://gcc.godbolt.org/z/oPrsoKx1E (no optimization)
https://gcc.godbolt.org/z/nTq9K7jY7 (-O3 optimization)
What is interesting is that in the -O3 optimization the two programs produce identical assembly.
With no optimization the first program calls:
call operator delete(void*, unsigned long)
But in the optimized code they have the exact same assembly. Clearly, the second program leaks while the first doesn't.