Matlab causes cudaMalloc error Ok, but the question is why?

Hi all,
I’ve already seen several topicslike this one but I think that this problem is not solved.
Ok, let’s define the problem.

  1. You make a matlab function.
  2. In this function, you do a “for” loop.
  3. In this loop, you call a CUDA function (like with mex functions).
  4. Your CUDA function does some cudaMalloc and cudaFree.

After several iterations, there is an “unknown error” during the cudaMalloc. The problem is that the memory whished is far less that free memory. Because it is in a loop (already done several times), there is theoretically enough linear free memory to correctly do the cudaMalloc.

I really don’t understand why this problem appears. Generally, the solution given is to “allocate the memory once and free it when the mex file is unloaded”. This solution is, according to me, not correct. First because with some problems, this technic cannot be used, and second because it can fix the memory allocation problem but it does not explain why the problem appears.

I really wnat to understand.

Thanks for the help!