Showing results for 
Search instead for 
Did you mean: 

General Protection Fault

Go to solution

Hi everyone,


This is probably an issue which is easy solved by you guys but I can' figure it out.

I'm running the same code, once when i execute it directly in main and the other when I'm calling it using a function.

The one in the main works, but not the other one. I'm not suer what I'm doing wrong here.

When leaving the function, I get an General Protection Fault which dosnt say much.


This is the main function, where I first INIT, then get the value from ReadAnalogInput.

This works fine!




This is the other case, where I call a function which is defined in another file.

There I'm doing the same thing, the values i correct but leaving that function (I'm at the breakpoint in red), it crash.


I have uploaded the project if that helps.

What's wrong?!


0 Kudos
Message 1 of 4



I will update the issue since it will help understand the error:

This is the main code:



In the Test Function I do following:



Where (*Tests) is defined as:

int short (__stdcall *Tests)(int channel, double *value);  

and loaded as:

(Tests = (void*) GetProcAddress(dllHandle, "Test"))


And the DLL is just setting the value to 5.0



It's crash if I call this function from main, but works if I execute it directly from main. Easier to understand what I mean with that from the first post.

I simply dont get what's wrong here.

0 Kudos
Message 2 of 4
Accepted by topic author viktor_morin


int short (__stdcall *Tests)(int channel, double *value);  


int short (__cdecl *Tests)(int channel, double *value);  


I'm not 100% sure, so if someone with knowledge would like to explain what's happening I would be more than happy.

I started to read here and just test myself forward:


But I still don't get why it works when I called the function directly in the main, compared to failed when called inside a function.


0 Kudos
Message 3 of 4

To answer your last question:

You are trying to retrieve a parameter from a function that has been passed by value. When passing by value, c puts a private copy of the parameter value on the stack when calling, and DISCARDS it when the function returns. Pass a pointer to the variable instead.

0 Kudos
Message 4 of 4