While debugging a program of mine I discovered that system (gui) events are being processed inside of calls to the serial port library. I did not realize that this was the case, and am somewhat stumped as to how to proceed now. Here is my (simplified) main loop:
while (!done) {
UpdateFromSerialPort (&someDataStructure);
ProcessSystemEvents();
}
One of the menu items in my program is to load a new file. In doing so, it frees the existing data structures, and mallocs new ones to match the configuration. Sometimes when I click that menu item, it is processed not during the call to ProcessSystemEvents where I would expect, but in ComWrt within UpdateFromSerialPort (according to the callstack in the debugger). This causes the data structures to be reallocated and when ComWrt returns, UpdateFromSerialPort will attempt to access the old data structures that were passed to in, resulting in a memory access error. I have noticed this problem in calls to some other CVI functions as well.
I'm not sure how to deal with this. Normally for single threaded (callback-based) programs, you can just assume that each each callback is atomic (won't be interupted by other callbacks), and use that fact to protect access to shared data structures. However if a bunch of these CVI function calls are potential entry points for callbacks, that completely blows that strategy out of the water. For multithreaded programs, I would simply put a mutex around all the accesses to the data structure, but that also won't work in this case, as is would cause a deadlock in the the nested callbacks.
Is there a way to disable processing of system events within all CVI function calls other than ProcessSystemEvents? Is there a list of CVI library calls that allow processing of system events in them, so I can know to avoid them?