This isn't a memory leak. It is a dangling pointer. foo()
returns a pointer to memory that is no longer allocated. It is undefined behavior to try to write or read from memory using this pointer.
A memory leak is when you have allocated memory that is no longer accessible. For example:
void foo()
{
char *buff = malloc(10);
}
10 bytes have been allocated, but there is no way those 10 bytes can ever be freed.
In your case, you have the opposite problem. You have a pointer to memory that has been freed automatically by going out of scope. Once memory has been freed in this way, you are no longer allowed to try to access it in any way.