Debugging mex-files. Visual Studio breakpoints not working.

For the longest time, I have been able to debug mex-functions using Visual Studio. In the past it often required a little dance of rebuilding and reattaching to Matlab, but it eventually works and breakpoints set in VS are tripped when the mex-file is called.
Lately, I find this is far less reliable and most times the mex-function executes without triggering a breakpoint. Even with repeated calls to clear mex, reattaching the debugger, etc.
My environment:
  • Visual Studio 2022 (Community)
  • Matlab 2022b
  • Mex-functions compiled with the -g flag.
  • Working in build directory (Matlab)
  • Build directory open (VS)
  • Note: This is not set up as a VS project or solution.
Typically I use my own project, but I've verified that the yprime.c example has exactly the same problem.
Working example:
copyfile(fullfile(matlabroot,'extern','examples','mex','yprime.c'),'.','f')
mex -g yprime.c
pause;%Open yprime.c in VS, Set breakpoint in VS attach to Matlab process
yprime(1,1:4)

回答 (1 件)

Sarthak
Sarthak 2023 年 5 月 15 日

0 投票

Hi John,
I tried to recreate the issue with your specified environment. The mentioned example yprime.c is running fine with the breakpoints behaving as expected. There may be some other issue why your breakpoints are not behaving as expected. Please verify that you have attached the MATLAB process correctly. If the issue persists, Microsoft Help Resources will be better suited to help troubleshoot these types of issues.
You can also refer to this answer for your reference.

3 件のコメント

John
John 2023 年 5 月 17 日
Strangely enough, after a reboot, I can no longer replicate the issue. I followed the steps I outlined both times and got different results. The only thing different is that the Matlab processes (and Windows) had much shorter uptimes (I will tend to leave Matlab open and running for weeks at a time).
Unfortunately, there is nothing so frustrating as an intermittent bug. As much as the debugging works for the moment, I think that there is something going on. Debugging has always been temperamental (often requiring recompilation, reattaching, 'clear mex' calls, etc.), and anecdotally, it seems to be getting worse with more recent releases.
They're not really true solutions, but rebooting and/or restarting Matlab are potential workarounds at least.
Jesse Hopkins
Jesse Hopkins 2023 年 10 月 27 日
I echo your sentiment John. For me, I have to use a procedure like:
  1. Start Matlab
  2. Compile mex file in debug
  3. Attach VS, set breakpoints
  4. Run mex file
  5. Break points work! (yay)
  6. Do some coding in the *.c file
  7. De-attach VS (to release *.mex file)
  8. Recompile mex file in debug
  9. Re-attach VS, set breakpoints
  10. Run mex file
  11. Drat, breakpoints no longer working
  12. Close Matlab, start new session, repeat steps 1-5, which works
I know that I used to be able to do just steps 1-9 circa 2015..
John
John 2023 年 10 月 27 日
Jesse,
Yes, that sounds about right. There seems no rhyme or reason to when it finally 'works' but it has certainly grown worse over the years. I don't find the the vague 'Blame Microsoft' exaplanation a particularly compelling response.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 5 月 10 日

コメント済み:

2023 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by