To be frank, I would say this is quite normal, not strange at all
The access violation message simply indicates that, your program was trying to access some momery, which was not allocated for it or had been freed already. (In this post, whenever I say "your program", it does not just mean the code written by you, but includes all the packages you used, especially your profiling package.)
Go down to the bottom, Perl program is c program any way. In c, a small careless usage of pointer or any similar mistake, can easily mess up your memory space. It is just so easy.
Come from a c background, I am not surprised at all to see, that a really slight change to your program, such as add a print statement, or comment out one single line, can "fix" the problem. I have seen this for so many times already. But for sure, that is NOT a fix at all. What happened was that, that small change slightly rearranged your memory space, although your pointer still pointed to a wrong place, but lucky enough, that wrong place was allocated for your program, so your program has every right to access it.
Update:
THE REAL PROBLEM IS THE WAY YOU THINKING.
You first placed a very strong assumption there, saying that Perl is handling everything properly, and your profiling package is also handling things properly. Based on this, then you confirmed to yourself that the error is really strange.
The right way of thinking is the total opposite:
- First you see a problem, and the error message clearly indicates that it is a memory problem. SO THE PROBLEM EXISTS, THAT'S A GIVEN, THAT's THE FACT.
- Secondly, as you said, both your experience and my experience confirmed that memory problem comes and goes, and gives people lots of wrong impression.
- It only sounds strange if you have a deep assumption that things were being handled properly, there should be no problem. However, we all know that no program is 100% correct.
- Yes, it is a commented out line affected the result, so what? Do you know each detail about how it is being handled by Perl? Do you each detail about how it is being handled by your profiling package? There is one thing the same between us, I don't know the details, and you don't know either. Yet there is another thing different between us, you take assumptions, I don't take.
Also I want to point out that, memory problem is usually not caused by the direct line that trigger the error msg, but could be some line hundred miles away. The reason is that, the line making the mistake, does not neccessary to access unallocated memory itself, but later another victim will access some unallocated memory because of that earlier mistake, and trigger the msg.
Update 2:
You said: "In all other circumstances it runs flawlessly?"
I have two questions for you:
- Is it really "flawlessly", or just did not hit the flawed spot? Your program passed 10,000 test cases successfully, can you say for sure that it will pass the 10,001th test case? We know what the answer is. To pass 10,000 test cases flawlessly, is TOTALLY different from "the program is flawless". ANY judgement saying "there IS a flawless program", itself IS flawed.
- Even if it is really true that, "In ALL OTHER circumstances it runs flawlessly", does this make it ture that "In ALL circumstances it runs flawlessly"?