accumulating thread count after calling mex file multiple times

1 view (last 30 days)
I have written a mex function that opens a W32 window as a child to a matlab window. And a second function that closes this window.
After calling these functions repetitively (20+) without errors, matlab becomes unresponsive after 5 to 10 minutes. Even if i call drawnow in between the other calls. The only thing I notice is, that after each call to my mex functions the number of threads associated with matlab increases (>250).
I am not creating threads in my mex function and after creating a window in the first call, I reuse the handle to the window.
Question, why does matlab freeze after some time , and why is the number of threads increasing.
  1 Comment
Jan on 9 May 2017
Without seeing the relevant part of the code, it is impossible to guess, what happens.

Sign in to comment.

Answers (1)

Chris van der Togt
Chris van der Togt on 10 May 2017
Edited: Chris van der Togt on 12 May 2017
I've not put any code because it would have been too much, but I think I have solved the issue.
The thing is that in addition to creating a w32 window (which is no problem at all), I was using this as a directx render target.
IN the initializing call of the directX device I obtained a pointer to the back buffer, but forgot to release the pointer. This leads to a silent increase of thread use in matlab, which ultimately leads it to hang.
ID3D11Texture2D* pBackBuffer = NULL;
hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
if( FAILED( hr ) )
return hr;
hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
pBackBuffer->Release(); // * *critical part I forgot.**
if( FAILED( hr ) )
return hr;


Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!