0

I'm not sure how language specific this is, but in PHP in particular, can you leak memory in the call stack if some functions never return?

For example, suppose you have a long function with redirect() (or gotos in C) statement(s), how does the language or environment know to delete that frame of the stack? Is there a method it uses to determine a stack frame will never be returned to?

  • 1
    What exactly do you mean by `if functions don't return`? What does the function do if not return? `how does the language or environment know to delete that frame of the stack? Is there a method it uses to determine a stack frame will never be returned to?` This doesn't make any sense. The function either returns or it doesn't, in which case it's still running. There is no way of not returning and not running either, unless you `exit`, in which case the whole VM is destroyed. – tkausl Mar 28 '19 at 14:53
  • For example, a redirect() statement in PHP or a goto in C. This means the function doesn't return, right? As far as I understand the CPU would branch to some other thread of execution. Does the PHP environment maintain its own call stack? – Anthony De Vellis Mar 29 '19 at 13:40
  • Whats a redirect statement? – tkausl Mar 29 '19 at 13:51
  • redirect() in PHP stops execution on the current PHP script and directs to another page with its own PHP script. So far I think the PHP zend engine maintains a stack for each page that is loaded. Will keep researching – Anthony De Vellis Mar 29 '19 at 14:49

2 Answers2

0

If your function does not return anything is some situations, then that sounds to me like a bad design issue. And no it should not cause any memory leak just by not returning anything.

Whether the return value of your function ends up being used or not, you should either always have a path that return something (null, false, or any value). Or you could never ever return anything ... in which case your function will be more like a method or a procedure as other programming languages would refer to it.

If you are having a memory leak in the function you are working with, than you need to scrutinize the code of the function to see what could be causing the leak.

asiby
  • 3,229
  • 29
  • 32
  • For example, a redirect() statement in PHP or a goto in C would cause the function to not return. Or if the process in which the function is running is sent a SIGKILL, but I think I know what happens in the last case. This is not related to anything practical, I'm just curious. – Anthony De Vellis Mar 29 '19 at 13:41
  • Good point regarding a redirect. Someone could be redirected to an authentication screen for instance. But SIGKILL is not a good example as a program that is kill is just killed regardless. At that point, nobody will care about memory leaks. – asiby Mar 29 '19 at 14:04
  • Yeah I understand what happens when a SIGKILL is sent. Process is cleared out of memory and its virtual address space is reclaimed by the OS. I'm not sure about redirect() or gotos yet though. In PHP, I think so far, that the zend engine maintains a call stack for each page. If you try to infinitely recurse on a page, you get an Exception when the stack reaches 256 calls. But you can go to 255 calls, redirect to a different page and then do another 255 calls. So I suppose the local page stack is just deleted when a new page is loaded, preventing leaks across the whole app – Anthony De Vellis Mar 29 '19 at 17:31
  • If you redirect away, the script that caused the redirection will run until the last line of code and the process exits. Unless you have an infinite loop. – asiby Mar 30 '19 at 02:16
0

This question answered by Memory leak on the stack Note: I did come across that question before I posted this one but I didn't understand the answers until doing more research.

Tl;Dr: PHP maintains a stack for each page. In C/C++ "automatic duration" storage is cleaned up automatically by the compiler telling the operating system to delete automatic duration allocations when program execution moves outside the scope of those allocations.

So I guess the compiler can detect execution paths that result in the function not returning and also 'marks'(?) those paths as moving out of scope in the same way that returns are marked as moving out of scope.

https://en.cppreference.com/w/cpp/language/storage_duration