Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breakpoint not hit in managed code

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(..).

like image 519
Grhm Avatar asked Dec 11 '22 20:12

Grhm


2 Answers

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.

like image 156
Hans Passant Avatar answered Dec 21 '22 12:12

Hans Passant


For me, the key was to change debugger type from Auto to Mixed, but for the startup application, not the library which contained the C++/CLI code (which is what I was trying to do).

like image 25
Dženan Avatar answered Dec 21 '22 12:12

Dženan