CUDA MEX library crash in MATLAB

I encountered a problem with CUDA library initialization in MATLAB environment. When MATLAB code repetitively calls my CUDA MEX file (in a loop, for example), CUDA MEX file crashes after first few times it is called. I discovered this problem in case of CUFFT library that failed to initialize and returned an error, but I also get similar behavior with CUDA code that does not include CUFFT. It seems like the problem is related to modular structure of my CUDA code (where CUDA MEX file imports several external C files with code via ‘#include’ statement), because simpler versions of that code (all in one file) seem to work fine. Clearing MEX files from MATLAB workspace via ‘clear functions’ command after every call to CUDA MEX file fixes the problem, however, this significantly slows entire application down. I would appreciate any help.

Since my program runs inside a large MATLAB loop, the crash could be related to the problem with CUDA malloc inside large MATLAB loops, described here:
[url=“http://forums.nvidia.com/index.php?act=ST&f=71&t=62562”]The Official NVIDIA Forums | NVIDIA
I appreciate any suggestions.

It seems like my problem went away when I fixed a race condition bug.