Breakpoint not hit in managed code

c++-cli, debugging, managed, unmanaged

Solution

Since your main EXE is a native program, it is likely that the debugger starts up in unmanaged mode and will therefore not support setting breakpoints on managed code. Project + Properties, Debugging, Debugger Type setting. Change it from the default of Auto to Mixed or Managed Only. Mixed debugging only works in 32-bit mode.

UPDATE: starting with VS2012 you also have to force the debugger to use the legacy managed code debugging engine, the one that still supports C++/CLI. Tools > Options > Debugging > General > "Use managed compatibility mode" setting.

Problem

I have inherited an application consisting of a number of C#, C++/CLI and native C++ projects. The app starts as an MFC application but loads the CLR during startup (via a process I'm not sure I fully understand yet†). I've found that I can place breakpoints in native C++ code and that these work as expected. However, breakpoints in managed code do not work. In C# I get: "The breakpoint will not currently be hit. No symbols have been loaded for this document". In C++/CLI I Get: "The breakpoint will not currently be hit. No executable code is associated with this line. Possible causes include: preprocessor directives or compiler/linker optimizations". I can even set two breakpoints in the same C++ file and have only one work, e.g. ``` #pragma unmanaged int CMyClass::UnmanagedFunc() { // Breakpoint here works return 1 } #pragma managed int CMyClass::ManagedFunc() { // Breakpoint here DOES NOT WORK!! return 2 } ``` The project settings for "Enable unmanaged code debugging" (within the managed library projects) has no effect on these breakpoints. Is there some setting or config or something that I do do to allow me to interrupt and step through the managed parts of the code base? †: The process loads `mscoree.dll`, and involves a complicated routine including `CLRCreateInstance`, `ICLRMetaHost`, `ICLRRuntimeHost`, `GetRuntime(..)`, `Start()` and `ExecuteInDefaultAppDomain(..)`.

Original source