LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Error if code is inside a function

Solved!
Go to solution

The following problem occurred inside a program I have written. I will try to explain it in a short version of the whole program:

 

I have a control-Callback for a button where i wait for EVENT_COMMIT. If i press the button I call a function where I handle different things. Inside of this function there is a Fmt() command where i get a General Protection Fault.

 

In short it looks like this:

int CVICALLBACK Main_CALLBACK_Test (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{

if(event == EVENT_COMMIT)
{

if(control == MAIN_Start)
{

Function_Call();

}

}

return 0;

}

 

void Function_Call (void)

{

char cDummy [50] = {0};
Fmt(cDummy, "%s<%s", "Test for Text message");

}

 

This behavior does not only happen with the Fmt(). I also get errors with for example SetCtrlVal commands or whole function calls. All of these errors only occur, if the code is inside of a function. If I put it outside, everything works fine. The function itself is declared in an .h-file which I include in the main-program. 

 

Please someone help me with this issue.

My System is Windows 10 with CVI 19.0.0 

0 Kudos
Message 1 of 3
(1,998 Views)
Solution
Accepted by topic author Marc1234

Update: I found the reason for this strange behavior and I want to write it down if anyone has the same problem.

 

In the last months I`ve been in the habit to declare my prototypes for functions without the transfer parameter in a header file which I include at the beginning of the c-File. Something like this:

 

Prototype in header: 

void Testfunction ();

 

Function in c-File:

void Testfunction (int iTest, char * cTest)

{

}

 

This worked fine for me and had the advantage of not changing the header file every time you add or subtract a transfer variable to your function. Additionally you are able to omit variable(s) if you do not need them at some other places inside your code. You just have to be careful that you do not transfer more variables than you defined in the function. 

 

Nevertheless, this lazy habit seems to be a problem for CVI if you put code inside of sub-sub-functions. In my example the code worked fine in the first function call. If I put it in another function call inside the first function CVI got GPF`s and other errors. The strangest one was an error at SetCtrlVal where i wanted to fill a text massage on my panel like this:

 

SetCtrlVal(iPanel, iControl, "Testtext"); 

 

The error CVI gave me was: Fatal-Runtime-Error: Found ", expected pointer to char. 

 

Hope this helps for others if they have same strange behavior. 

0 Kudos
Message 2 of 3
(1,904 Views)

The bug is not that you get these errors. The bug is rather that you can define incomplete functions in a header. For the compilation of the implementation it’s not a problem but for the compilation of callers it is as the C compiler has to go by the declaration of the header and can’t know about how the function is implemented. So it accordingly stuffs as many parameters on the stack as your code passes to it and if your function tries to pull more from the stack it references garbage.

And if you change the default calling convention to stdcall you even corrupt the stack as the caller pushes n parameters on the stack and the function clears m parameters from it!

 

The type safety provided by complete function declarations outweighs any perceived advantage of ease of quick and dirty modifications manyfold!

Rolf Kalbermatter
My Blog
Message 3 of 3
(1,878 Views)