From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

General Protection Fault

Solved!
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!

 

image.png

 

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.

image.png

I have uploaded the project if that helps.

What's wrong?!

 

0 Kudos
Message 1 of 4
(3,644 Views)

**UPDATE**

 

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

This is the main code:

image.png

 

In the Test Function I do following:

 

image.png

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

 

image.png

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
(3,626 Views)
Solution
Accepted by topic author viktor_morin

Change:

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

To:

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:

https://docs.microsoft.com/sv-se/cpp/cpp/cdecl?view=vs-2017

 

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
(3,617 Views)

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
(3,519 Views)