opening .h files with a running program

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Re: opening .h files with a running program

Postby arisme » Fri Feb 05, 2010 11:56 pm

ZER0 wrote:i get EStackOverflow error after i use the function for a certain amount of times (51315 to be exact)....

Is the error thrown after 51315 executions of the whole of the code you posted, or after 51315 calls to function k()?

Are you working in C++Builder? (i.e. Can you run in debug mode, apply breakpoints to selected statements, and then step through the code inspecting values?)

Idle curiousity, "funkcija" - Latvian? Lithuanian?

Aris
arisme
BCBJ Master
BCBJ Master
 
Posts: 357
Joined: Thu Jun 07, 2007 9:35 pm
Location: UK

Re: opening .h files with a running program

Postby gambit47 » Sat Feb 06, 2010 12:11 am

ZER0 wrote:But now i got another problem -i get EStackOverflow error after i use the function for a certain amount of times (51315 to be exact). Increasing gg or og sizes changes nothing.


Stack overflows are commonly caused by recursive loops that run too many times internally, when each iteration puts more and more local variables on the stack until there is no more space available.

The other possibilty, since you are importing the function from a DLL, is that you have probably used the wrong calling convention in your funkcijah type and are thus mismanaging the stack, where each call of the function is pushing parameters on the stack that the function is not clearing correctly on exit.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: opening .h files with a running program

Postby ZER0 » Sat Feb 06, 2010 5:19 am

gambit47 wrote:The other possibilty, since you are importing the function from a DLL, is that you have probably used the wrong calling convention in your funkcijah type and are thus mismanaging the stack, where each call of the function is pushing parameters on the stack that the function is not clearing correctly on exit.

That probably is the case, because, when i use included .h file for this code, it works fine, and when i use a static .dll file made by builder, it also works fine. The problem probably is with the calling of the .dll this way it is done here.

arisme wrote:Is the error thrown after 51315 executions of the whole of the code you posted, or after 51315 calls to function k()?

The error occurs after 51315 function calls.

arisme wrote:Are you working in C++Builder? (i.e. Can you run in debug mode, apply breakpoints to selected statements, and then step through the code inspecting values?)

Yes, i'm using c++ builder and i can run in debugging mode, but as far as i've checked the values of gg and og are ok all the time, it must have something to do with the wrong cll of dll.
arisme wrote:Idle curiousity, "funkcija" - Latvian? Lithuanian?

Its Lithuanian. Pretty close call, you must have met some of our people. I was too lazy to translate the default name in the code :)

Here is the beginning of the .dll which is compiled and used when the error occurs:
Code: Select all
#include <windows.h>
BOOL WINAPI  DllMain (HANDLE hModule, DWORD dwFunction, LPVOID lpNot) {return TRUE;}
_declspec (dllexport)
// -active visi
void funkcija(char *gg,char *og,int in,int on,int *gerai)
{.....
}
ZER0
Active Poster
Active Poster
 
Posts: 15
Joined: Wed Nov 18, 2009 12:20 pm

Re: opening .h files with a running program

Postby gambit47 » Sat Feb 06, 2010 7:41 pm

ZER0 wrote:That probably is the case, because, when i use included .h file for this code, it works fine, and when i use a static .dll file made by builder, it also works fine. The problem probably is with the calling of the .dll this way it is done here.


In your earlier code snippet, you showed the declaration of your funkcijah typedef as follows:

Code: Select all
typedef void (__stdcall *funkcijah)(char *, char *, int, int, int *);


However, you have now showed the implemenation of the funkcija function as follows:

Code: Select all
void funkcija(char *gg,char *og,int in,int on,int *gerai)


Notice the difference? Your typedef has an explicit __stdcall calling convention specified, whereas the actual function has no calling convention specified at all. In the absense of a calling convention, __cdecl is commonly used, not __stdcall (this is configurable in the project options). That mismatch would certainly account for your stack overflow. __stdcall requires the called function to clean up the stack, whereas __cdecl requires the caller to do it instead. So you are telling the compiler that the imported function will cleanup the stack when it really does not, and the function is expecting you to cleanup the stack when you are really not.

ZER0 wrote:The error occurs after 51315 function calls.


The function requires 24 bytes (20 bytes for the parameters, and 4 bytes for the return address) to be pushed on the stack every time it is called (not including any additional stack space it uses interally). The 4 bytes of the return address are cleanup up by the CPU automatically, but because of your calling convention mismatch, the 20 bytes used for the parameters are being leaked every time. After 51315 calls, you have leaked almost 1 full megabyte of stack space. Threads have a limited stack space alloted to them (specified in the project options). A stack overflow means you are exceeding that limit.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: opening .h files with a running program

Postby ZER0 » Sun Feb 07, 2010 4:59 am

Thanks for the answer, now it works fine.
ZER0
Active Poster
Active Poster
 
Posts: 15
Joined: Wed Nov 18, 2009 12:20 pm

Previous

Return to Technical

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron