MATRIXx

cancel
Showing results for 
Search instead for 
Did you mean: 

Freeing dynamically allocated memory

How do I free UCB dynically allocated memory at exit time? Have startup block calling allocation UCB.

Thanks,
Tim
0 Kudos
Message 1 of 2
(7,187 Views)
Tim,
It depends on which function interface you are using for the UCB. There are two function interfaces. One is for use with SystemBuild only (sysbld\src\usr01.c) and makes more information available about the simulation. The other is the interface that allows you to use the same UCB for both SystemBuild and AutoCode (case\acc\src\sa_user.c).
If you are using the SystemBuild only version then there is a IINFO array that is passed to the UCB with mode information. One of the modes is LAST, which is called when the simulation is done. You can look for this mode and perform any clean up during that call. One thing with this approach is that LAST does not always get called, such as when a Interactive Simulation is aborted in the middle of a run or when a simulation is stopped in the middle by the Stop block. In those cases you would need to rely on the next approach.
When using the SystemBuild and Autocode version then you can not detect the end. However the simulation runs in its own process (simexe.exe) and so when the process is terminated any dynamically allocated memory is cleaned up. After a simulation is done simexe will remain in memory so that it can be called again if a new simulation is done with out changing the model. There are two ways to terminate the simexe process, one being to quit xmath and the other to run the command 'undefine simexe' in xmath. To make things even cleaner during the INIT mode you can check the pointers. If they are valid you can continue to use them, or free and reallocate them. If they are not valid then you can allocate the necessary memory.
For more information about UCBs and modes see the SystemBuild Users Guide chapter 14, and AutoCode Rerference Manual Chapter 2 (section UserCode Blocks).

Carl L
National Instruments
0 Kudos
Message 2 of 2
(7,166 Views)