How can a bug could occur on static, fixed input data only "sometimes"? The following is simply an example I have observed, but I am not interested in an answer specific to the GD library.

I have some PHP code to render SVG files, utilizing the GD Graphics library. It often stops working, and no error is given. The strange thing is that after 2-3 tries it works and keeps working with no code changes. This is very strange to me because I understand that assembled code follows in sequential order and operates on certain external inputs. In this case, the known input is a static file.

My question is, how is this possible (from a general, theoretical point of view)? What could the GD library (or any other code) be doing to cause such irregular behavior? How could code run differently based on how many times you ran it, if the input stayed the same?

  • 2
    Hangs? A memory leak perhaps?
    – yannis
    Dec 29, 2015 at 1:54

1 Answer 1


When I understand you correctly, your question is not specificially about GD, but how in general a function with fully deterministic requirements can show non-deterministic behaviour: given exactly the same input, the output (or in this case the behaviour) is different, though there is no obvious cause like a clock-dependency or random generator.

Whenever you run a program or a function on a system, even when the code is executed in a separate process, it is never executed in perfect isolation - there is always some environment around it, lots of other processes running simultanously at the same machine. There are always shared resources like main memory, disk space, processor cores, gfx card memory, global environment variables, and so on, which are not to be expected in exactly the same state when you run the program twice at different points in time. That alone can be the cause of the non-deterministic behaviour you are observing.

Furthermore, GD is written in C, which is a language where one can create "undefined behaviour" in several ways. For example, this can happen accidentally by forgetting to initialize a variable before usage (not that this is really the case for GD, just take this as a hypothetical scenario). This makes it possible the value of a variable can depend on the state of the shared resources I mentioned above. And assumed the GD lib contains such a piece of code somewhere in the SVG rendering code, or in its PHP binding, then it I can imagine that it could lead exactly to the behaviour you described.

  • Thanks. Sounds like what is happening. So, for example, an uninit variable might sometimes have the value of 0, othertimes, MAX_INT, thus causing the program to run forever by making, for example, an extremely large image, or by not terminating at the proper time, causing extreme computations or an infinite loop. Dec 30, 2015 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.