Felipe C - 2021-03-23

I've found a problem with the LabVIEW Snap7 server interface when trying to create and use a couple of DB's at the same time. Somehow when writing or reading data from these DB's the values were mixed between them.

After a hard time debugging the problem (several months actually) I could figure out what was happening. The LabVIEW interface just wraps the C++ API in way the the dynamic library get understood by the call library function node inside the LabVIEW code. It works nicely except for one kind of call.

The Snap7 C++ API, as a common programming practice, lets the caller application to allocate the memory when it needs a block memory, for example to allocate a data block as I mentioned. However, the call library function node doesn't really allocates this memory in heap as malloc does when redirecting a memory block passed as input from another block , it just copy the data to the called function and allocates it in a temporary buffer. What happens is that when more than one data block is create it is not guaranteed that all of them are going to be allocated in separated memory areas. Its very common that couples of data blocks turn to share a same area in memory.

I did some research on call library function node but could not realize how to avoid this problem through LabVIEW code. So I manage to make the Snap7 LabVIEW wrapper do the job for LabVIEW. I created a simple dynamic memory allocator in the LabVIEW wrapper to receive the data from the created area, store it in heap and pass the correct pointer to the API.

As bonus I could implement a feature that was missing. There isn't anyway to server direct access its own blocks in memory as call library function node doesn't returns a pointer to the blocks. Now I can direct write or read server objects in memory.

Regards