LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

declspec(thread) in call library function causes invalid access to memory location error

Labview version 7.0 on Windows 2000

I have a dll from which I'm logging messages to a file. All functions have to be thread safe, and the logging occurs in a function that looks like:

void log(const char * szFormat, ...) {
static __declspec( thread ) char szmsg[1024];
static __declspec( thread ) va_list ap;
va_start(ap, format); // <-- crash!
_vsnprintf(szmsg, 1023, format, ap);
va_end(ap);

... (write resulting string to file, stdout, ...)
}

This works outside of Labview with no problems; however every time this is encountered in the context of a call library function node I get a message box in Labview like:

Error loading "c:\dev\mydll.dll". Invalid access to memory location.

If I remove the __declspec(thread) from the declarations of szmsg and ap, then the error goes away and Labview works fine. That's not satisfactory though since it's not thread-safe. I have two other options: (1) create a home-grown thread affinity solution, which is potentially buggy; or (2) remove the static keyword from the declarations of szmsg and ap, which is inefficient considering that this log function could be called many times per call library function node invocation.

Does Labview prevent the use of thread local storage in call library function nodes?

Thanks!
0 Kudos
Message 1 of 2
(2,961 Views)
This one is a little beyond me, so I took it to some of our R&D engineers. Without more in-depth testing, they were unsure of why LabVIEW would throw any errors. Although not explicitly unsupported, they agreed the use of thread local variables might not be the best idea. I will document the error and have them look into handling this situation in later versions of LabVIEW.

The general consensus, however, was that going with your second option would not noticeably increase execution time. Removing the static keyword is probably your best bet at this point.

Thank you for bringing this issue to our attention and helping us make LabVIEW a better product.
Robert Mortensen
Software Engineer
National Instruments
0 Kudos
Message 2 of 2
(2,943 Views)