Page 1 of 1

Run-Time Check Failure followed by Access violation

Posted: Fri Mar 27, 2009 5:45 pm
by Janschenkel
I'm writing my first external, on Windows, using Visual C++ 2008 Express with the aid of ExternalsEnvironmentV3. Just started debugging the work I've done so far, using the green triangle 'Start Debugging' button in VS.
One function call (to check if everything has been initialized or not) works just fine, but when I next call the initialization function, I first get:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
and then:
First-chance exception at 0x00000000 in Revolution.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x00000000 in Revolution.exe: 0xC0000005: Access violation reading location 0x00000000.
and then I basically have no choice but to kill Revolution.

Any pointers from more experienced external developers on how I can tackle this?

TIA,

Jan Schenkel.

Posted: Fri Mar 27, 2009 7:09 pm
by mwieder
Well, there's not a whole lot to go on there. From the looks of things you're dealing with a null pointer, and the fact that the stack pointer is messed up sounds like

A: you haven't declared the initialization function using the proper calling convention or
B: the engine's cleanup routine has freed a pointer to something in your code, probably the return string.

Either of those sound like possibilities?

Posted: Fri Mar 27, 2009 7:36 pm
by Janschenkel
Thanks for taking a stab at this. Having just re-ran the test after sprinkling the external code with breakpoints, and stepping through the code, I noticed that:
1) the ESP error actually happens three times, twice inside the external and then once more upon return from the external.
2) everything still seems to work correctly inside the external, so I must be messing things up in there somehow, in such a way that Revolution goes nuts.
3) the return value is a simple strdup("") like in the other examples, so I don't think that's the culprit.

A little more background information: I'm dynamically loading another .DLL, and then declare some function pointers based on the return value of GetProcAddress - and the debug session showed that the first two ESP errors appear whenever I'm calling these two function pointers.
No idea if it's relevant, but I created a "C++ with exceptions" style of external.

I am confident it's something stupid that I'm doing - the code is assembled from various example sources, so the odds are good that I've put it together incorrectly - my expertise is in Revolution, Progress ABL and Java, definitely not in C/C++

TIA,

Jan Schenkel.

Posted: Fri Mar 27, 2009 9:14 pm
by mwieder
I'm dynamically loading another .DLL, and then declare some function pointers based on the return value of GetProcAddress
That can get pretty dangerous if memory gets moved around. Are you locking the memory locations? Is there some reason you can't just call the functions in the other dll rather than hardwiring pointers to them?

Posted: Fri Mar 27, 2009 9:54 pm
by Janschenkel
Luckily, I only need those two function pointers during the initialization faze - the rest of the time I have the object those create, and call onto that object's methods. At any rate, I figured it out by taking another look around the web for more example code calling into that DLL.
So no more Run-Time Check Failures :-)

However, I am now getting other memory problems, so it looks like I'll need to take another look at how I put together my return values. It never seizes to amaze me how many *'s I have to or _not_ type in C - and it's 10PM now anyway, so I'll call it a day.

The ExternalsEnvironment that Mark Waddingham posted in his newsletter articles helps a lot for the setup. I do wish there were more rev external sample sources available to learn from, though...

Thanks for nudging me in the right direction. Indubitably, I'll be back with more questions in the near future.

Jan Schenkel.

Re: Run-Time Check Failure followed by Access violation

Posted: Sat Mar 28, 2009 1:54 am
by SparkOut
Janschenkel wrote:with the aid of ExternalsEnvironmentV3.
I've only got v2 from the articles. Can you advise the source of v3?
Thanks.

Posted: Sat Mar 28, 2009 6:32 am
by mwieder
Yeah - I didn't know about v3 either, but I just adjusted the url and grabbed it.