06-27-2022 09:26 AM - edited 06-27-2022 09:45 AM
Hello,
I have a function that is exported in a DLL and called by TestStand. One of the parameters to the function is CAObjHandle seqContextCVI, which allows TestStand to pass in the sequence context. Inside my DLL function, I pass this sequence context to one of the callback functions.
The callback functions, by definition, receive data in the form of a void pointer (*callbackData). I am sending the sequence context to the callback function in the form of this void pointer.
In the main function, I do the below code in order to turn SeqContextCVI into a void pointer:
void *callbackData = NULL;
callbackData = &seqContextCVI;
Once inside the callback function, I dereference this void pointer (after casting) to obtain the SeqContextCVI value. Below is that line of code:
CAObjHandle seqContextCVI = *((CAObjHandle*)callbackData);
It gives me a runtime error of "dereference of null pointer"
What am I doing wrong here? I believe that CAObjHandle is just an int type. Is it a pointer? If so, I could understand why the above operation does not work.
Solved! Go to Solution.
06-28-2022 05:40 AM
Hi,
I think that you are not doing any mistake, but are you sure that the address is valid when used .
Test code:
#include <cviauto.h>
#include <utility.h>
void CB(void* callbackData)
{
CAObjHandle seqContextCVI = *((CAObjHandle*)callbackData);
DebugPrintf("%d\n", seqContextCVI);
}
void main(void)
{
CAObjHandle seqContextCVI = 2;
void *callbackData = NULL;
callbackData = &seqContextCVI;
CB(callbackData);
}
06-29-2022 02:13 AM
Is the handle even valid when your receive it from TestStand in your function? You need to systematically debug that and go back from where you see the error to see where that problem occurs. How could you test that?
Simple, try to insert a function (to read some valid attribute of the context for instance) in your function from which you initialize the callback. Does it return an error?
If that shows no problem you need to show us more about how you prepare the callback pointer. The code as you show it seems to look harmless but if you do some more pointer arithmetic there that somehow involves stack variables the stack of your function will potentially be gone at the time the callback function gets fired. Does that callback get fired after your function has returned to the TestStand executor? TestStand my already have re/deallocated that context somehow.
06-29-2022 06:23 AM
I am not seeing the problem anymore. I think it was being caused by another issue.