Random Access Violation when reading a location

Hi,

I'm trying to solver an intermittent problem that is resulting in an "Access Violation Reading Location" error. My apologies if this is a common issue. I have tried searching for everything that I can think of, but haven't fond anything so far, and this is driving me crazy.

I am writing a C++ DLL which is called from Excel. This DLL uses a number of C++ libraries. The libraries are an open source project called Cantera. So I DO have access to the full source code and debugging info for both projects.

As I said, the symptom that I'm seeing is an unhandled exception when running the code while attached to the Excel process. "Unhandled exception at 0x08f2b8ff (Cantera_Toolkit.dll) in EXCEL.EXE: 0xC0000005: Access violation reading location 0x095e9000." I have the Cantera C++ library compiled in debug configuration. The DLL is also compiled in debug. The code that causes the error is in the library (not the DLL that calls he library).

When I get the error while debugging, first off it happens randomly. Secondly, the call stack goes back to an STL function. It is an inline function that computes a dot product. I've shown it below. When I look at x_begin, and x_end, and then look at the memory window, sure enough it looks like those locations are past the currently allocated memory. So indeed I should get an error. What's confusing me is that if I go up on level in the stack, to the code that calls the function "dot", then I see that the pointers that are given in the call are valid... So it looks like somehow "dot" is being called with valid inputs, but once inside "dot" the inputs are offset in memory space by some amount, maybe 75 bytes or so. Like I said, this works fine probably nine times out of ten.

Function where the error shows up
template<class InputIter, class InputIter2>
inline doublereal dot(InputIter x_begin, InputIter x_end,
InputIter2 y_begin) {
doublereal sum = 0.0;
for(; x_begin != x_end; ++x_begin, ++y_begin)
sum += *x_begin * *y_begin;
return sum;
}


Calling Function:
void State::setMoleFractions(const doublereal* x) {
int k;
doublereal sum = 0.0, norm = 0.0;
sum = dot(x, x + m_kk, m_molwts.begin()); << x, m_kk, and m_molwts.begin() are valid addresses, and seem to have valid structure&Data
for (k = 0; k != m_kk; ++k) {
m_ym[k] = x[k] / sum;
m_y[k] = m_molwts[k]*m_ym[k];
norm += x[k];
}
m_mmw = sum/norm;
}


Other Info:
- I was using VC7.1, and this issues started when I moved the projects to 2008 Express Edition. Perhaps that has something to do with this??
- I doubt that there's anything special about this function.
- I overloaded new & delete in my DLL in order to track when memory is allocated and deleted. I'm 99% sure that I've got no memory leaks, dangling pointers, etc.
- I seem to get this problem with any combination of release/debug on both projects.

That's about all I've got. Any suggestions?

Thanks!
-Michael
Try counting how many times the for in dot() loops before crashing. See if it matches m_kk. If it doesn't, it means the array is shorter than you think. If it crashes in the first loop, the pointer is probably invalid.
Helios,

Thanks for the suggestion. I stepped through the for loop in "dot" five times, and it looped in the for loop the correct number of times, m_kk=53. Then I took out the breakpoint in the for loop, and just hit F5 to step though the whole "dot" function. I counted 400 times going through "dot" with no issue. Then gave up and removed the breakpoint. The code completed successfully.

I ran the code probably 100 times more, no issue, then it finally hit the same error. After counting the number of times it goes through with no issue, it seems to be fine 99.99% of the time. unfortunately I can't seem to catch this error, so I've got to figure out what's going on.

Thanks,
-Michael
Sounds like memory corruption, to me. You must have done something wrong somewhere in the code, like deleting something you shouldn't have deleted, or overflowing a buffer.
Well, the mystery is a little less now. I hadn't realized that the x_begin was being iterated upon, so as soon as you run through the for loop once, it's no longer at the beginning.. So when the error is raised, its value doesn't match the value passed in from the calling function, and it shouldn't. That's what was getting more more than anything.

I figured this out by adding a counter to the for loop, so that when it finally crashed, I could tell how many times it had been through the for loop. I was assuming that it was the first time, but that wasn't true... So, good suggestion counting the # of times through the for loop, just had to not do it by hand so that I could tell how many times when there was an error raised half way through.

So, as you said, it was trying to read past the end of an array (past the fdfdfdfd in the memory window looking at the debug heap). This happens in the library that I'm linking to, not in the DLL. I suppose I could put a watch on the memory location, to see what is changing it. The problem with that is that I don't know what memory location I need to look at, because it should change each time the code executes, and it works 99.99% of the time. I'll have to see if I can't make it so that the memory location doesn't change so that I can put a watch in on that location...

Any suggestions would be helpful..

Thanks,
-Michael

So, good suggestion counting the # of times through the for loop, just had to not do it by hand so that I could tell how many times when there was an error raised half way through.
Um... Yes. I assumed you would draw the conclusion that you can make the computer count things.

Again, classical signs of memory corruption. The place where the program crashes can be completely different from the place where the bug actually is. I recommend using a memory debugger.
Topic archived. No new replies allowed.