Visual Studio C++ Cannot load Symbols and access violation when debugging - c++

I am having a very strange issue. I have a c++ project that was working just fine until today. Today I tried to run the visual studio debugger and at the very beginning of the main function, I get an access violation exception. The line where the exception occurs varies, though it is always in the first few lines of my main function. Visual studio claims the source file is not available.
I'm not sure what changed, although recently, upon opening the visual studio project, I was presented with a message about finding a suitable location for he pdb, which i naively clicked "ok" to.
In case this helps, the call stack is:
ntdll.dll!_RtlInitUnicodeStringEx#8()
KernelBase.dll!LoadLibraryExW()
KernelBase.dll!_LoadLibraryW#4()
kernel32.dll!#BaseThreadInitThunk#12()
ntdll.dll!__RtlUserThreadStart()
ntdll.dll!__RtlUserThreadStart#8()
Edit:
I tried a new empty project. Literally:
// TestProj.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main()
{
return 0;
}
and I get a similar error.

Related

Debugging into MFC header code does not work with Visual Studio 2019

TL;DR: Debuigging into MFC (CString) header code does not work on both my machines and as far as I can tell this is due to the peculiar way these headers are compiled.
Stepping through MFC header code when entered via disassembly works, but setting brealpoints does not work.
I'm looking for a workaround or at least acknowledgement of my analysis.
System:
Visual Studio 2019 Professional 16.9.6
Windows 10 / 1809 Enterprise LTSC
Setup: (I do apologize for this being rather long.)
Create a Visual Studio 2019 Example MFC Application Project (SDI App)
Make sure Enable Just My Codeis off under Options -> Debugging -> General.
Set the build configuration to Debug/x64 (does not make a difference, but let's all stay on the same page)
Navigate to MFCApplication1.cpp -> CMFCApplication1App::InitInstance()
Insert a CString init like this:
...
InitCommonControlsEx(&InitCtrls);
CWinAppEx::InitInstance(); // please put breakpoint 1 here
// Add this line and set breakpoints
CString this_is_text(L"Debugging into CString Header does not work!"); // breakpoint 2 here
Now, you can start the program under the debugger, and you should stop at the first breakpoint:
Now, make sure all symbols are loaded, easiest done via the Call Stack:
Just select all lines in the call stack window and hit Load Symbols in the context menu. Afterwards the call stack should look roughly like this:
> MFCApplication1.exe!CMFCApplication1App::InitInstance() Line 75 C++
mfc140ud.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00007ff7b5070000, ...) Line 37 C++
MFCApplication1.exe!wWinMain(HINSTANCE__ * hInstance=0x00007ff7b5070000, ...) Line 26 C++
MFCApplication1.exe!invoke_main() Line 123 C++
MFCApplication1.exe!__scrt_common_main_seh() Line 288 C++
MFCApplication1.exe!__scrt_common_main() Line 331 C++
MFCApplication1.exe!wWinMainCRTStartup(void * __formal=0x000000c2b7084000) Line 17 C++
kernel32.dll!BaseThreadInitThunk() Unknown
ntdll.dll!RtlUserThreadStart() Unknown
Now, you can try stepping-into (possibly F11) the CWinAppEx::InitInstance() function, which should work without a problem, landing you in mfc140ud.dll!CWinApp::InitInstance() Line 394 - this is OK.
Step out again, and then then try to step-into the CString ctor:
This DOES NOT work on my machine(s)!
What I can do however, is (from the point above) switch to disassembly view, step into the calls there and get into the header code this way:
I can then successfully step through (but never into) the MFC header code. Trying to set a breakpoint will result in the error:
The breakpoint will not currently be hit. No executable code of the debugger's code type is associated with this line.
Possible causes include ...
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\atlmfc\include\cstringt.h
And this is where I'm at.
Analysis:
What we can see from the MFC code is that we can step into "regular" cpp code, but as soon as we try to step into (or set breakpoint) code that is inside this CStringt.h it breaks.
Peculiar here: This is template header code, and still the executed code (as shown by the disassembly) is not in the user module but in the mfc###.dll! I think they do some clever tricks with the preprocessor (see defined(_MFC_DLL_BLD) and somesuch) which enables this multi use of the header file, and maybe, possibly this is also what breaks the debugger.
Question:
Is this a known problem, does this happen with all VS2019 installs, is there something peculiar to my setup?
Maybe fixed in a newer VS version?
Iff this is actually broken, what would be a useable workaround, other than constantly switching to disassembly view when into the MFC headers.
The most interesting answer here would actually be as to WHY this breaks - where does the debugger get confused? Is this a general problem with re-define-ing code when debugging library code?
The source shipped with MSVC does not match.
I think this happen, as DLLs got updated with Windows Update or a new vcredist, but Visual Studio includes are not updated. If you build with /MT or /MTd and link MFC statically, the problem does not persist.
Probably this can be reported to http://developercommunity.visualstudio.com if you care.
Workaround 1
Do steps described by #selbie:
Set a breakpoint on the line of code I want to step into.
When
the breakpoint is reached, right click in the editor window and select
"Go To Disassemly".
In disassembly mode, step over until you get to
a call statement. [...] You
can flip out of disassembly mode by right-clicking again and selecting
"go to source code".
(skipped the part not relevant to this issue)
Then pick up the location of the header manually, the debugger will tell that it does not match. The difference seem to be insignificant though, so the header is usable.
Workaround 2
Link MFC statically, compile with /MT or /MTd
Workaround 3
ATL has a similar CString that does not suffer from the issue:
#include <atlbase.h>
#include <atlstr.h>
int main() {
ATL::CString this_is_text("Debugging into CString header works");
}
Analysis went sideways at some point, but we finally found one part of the problem here:
The Require source files to exactly match the original version option:
was the problem, but in a very peculiar way:
When you do NOT require source files to match (that is, disable this default option), then the erroneous behavior of the OP occurs: The debugger can no longer match the symbols to the cstringt.h file.
Unfortunately, I had this disabled on both machines. Pulling in a third machine showed that we could set breakpoints (though F11 still does not work) and by comparing the xml export of the VS settings we found that this was different.
So, long story short: For us, to be able to set breakpoints in the (unmodified!) MFC header, requires us to enable the Require source files to exactly match .. option.
If the option is disabled, which would imply a more lenient behavior by the debugger, it no longer works.
And, yes, we double checked it's always the same source file at C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\atlmfc\include\cstringt.h
The mystery with step-into/F11 persists, but I guess this would better be taken to a separate question.
Uncheck the Enable Just My Code option in Tools->Options->Debugging
I know that works for c++ std:: library code debugging. The other technique I do, when I forget to uncheck this option, is similar to what you describe above.
Set a breakpoint on the line of code I want to step into.
When the breakpoint is reached, right click in the editor window and select "Go To Disassemly".
In disassembly mode, step over until you get to a call statement. That's typically the std library. Eventually, you'll navigate into a mix of assembly and system code sources. You can flip out of disassembly mode by right-clicking again and selecting "go to source code".

how to debug a c++ excel plugin's exception during excel exiting

I have an excel xll plug-in built in c++ (and with the help of xlw), which, runs pretty well except, when excel exits, exception happens occasionally.
My headache is that though it looks like some destructor issue, I can't see where it went wrong.
Excel window just closes then a Windows system error message pops out.
Even if I run it in Visual Studio debug mode, when the exception happened, it's already in the STL c++ code, also I can't see which part of my code, such as a destructor, is the root cause of the failure.
To be precise, Call Stack shows
[External Code] -> Excel.Exe -> [External Code] -> MSO.DLL ... repeat... OART.DLL... repeat ... ntdll.cll -> [External Code] -> _cexit() -> common_exit -> __acrt_lock_and_call -> ...
The first step with source visible is exit.cpp in C:\Program Files (x86)\Windows Kits\10\Source\10.0.16299.0\ucrt\startup\exit.cpp,
extern "C" void __cdecl _cexit()
{
common_exit(0, _crt_exit_full_cleanup, _crt_exit_return_to_caller);
}
Have you tried setting a conditional breakpoint in Visual Studio settings to break on any exception (or the specific exception you are getting)? You can enable this just before you exit Excel. This might help track down the problem and give you the call stack in the call stack window when the breakpoint hits.
Also make sure to check if your symbol files (.pdb) are getting loaded for your code and any third party dependencies. Another thing that can help is to specify the Microsoft public symbol servers so that the Microsoft system pdb’s are loaded as well as mentioned in this article.

memcpy.asm not found error

I am working in Optix Using Visual studio 2013 platform, I have been working over month , suddenly I got this error, "memcpy.asm not found".
I found this file in Visual studio folder but it says "The source file is different from when the module was built"
Look carefully at the error, often this comes up as a secondary message because the debugger is trying to load "memcpy.asm" in order to show you debugging information because a memcpy failed, not because your program is actually missing "memcpy.asm"
Heres someone with a similar issue, in short - check the stack trace to see where the issue originated in your code, probably with trying to copy memory to an uninitialized pointer or something.

Panda3D and load_model c++

I am new to panda3d and am trying the hello world.
I have gotten the program to run with just:
PandaFramework framework;
framework.open_framework(argc, argv);
WindowFramework *window = framework.open_window();
framework.main_loop();
framework.close_framework();
return 0;
however, if I add in :
NodePath environt = window->load_model(framework.get_models(), "panda-model");
environt.reparent_to(window->get_render());
environt.set_scale(0.25, 0.25, 0.25);
environt.set_pos(-8, 42, 0);
before the main_loop(), it won't compile. I have the panda-model.egg.pz in every folder of my c++ project but every time my program breaks on :
environt.reparent_to(window->get_render());
The excat message is: Unhandled exception at 0x0152a317 in Irr.exe: 0xC00001A5: An invalid exception handler routine has been detected.
It compiles fine the only wraning being that it can't find the pdb files but that doesn't matter.
Any ideas?
Btw, im using MSVC++ 2010 and panda3d 1.7.2
The only error I can find is : First-chance exception at 0x00bfa317 in Irr.exe: 0xC0000005: Access violation reading location 0x737265db.
Unhandled exception at 0x00bfa317 in Irr.exe: 0xC00001A5: An invalid exception handler routine has been detected.
The program '[8476] Irr.exe: Native' has exited with code 0 (0x0).
which I'm pretty sure is still rather vague. I'm sorry but its all I can find. I'd be happy to describe it more though if necessary.
Microsoft Visual C++ 2010 is not supported by current versions of Panda3D. You will need to use 2008 or compile the Panda3D SDK from source against MSVC 2010. (This is not as straightforward as it sounds, as you will also need to compile some of the thirdparty packages to be compatible with 2010. However, it is possible.)
If you insist on using 2010 instead of 2008, this forum post may be helpful:
https://www.panda3d.org/forums/viewtopic.php?t=12663

F11 Debug line by line only my .cpp?

I am developing an OpenCV application and I need to debug my code (Visual studio 2010, opencv with cmake).
I would like to debug line by line to see where exactly my app crashes. I am trying to use F11 the problem is that F11 shows me external code (opencv libraries, std libraries etc).
Is there any other way to don't put lots of breakpoint all over my code?
int main(void) {
vector<int> myVec;
myVec.push_back(1);
//> Do other stuff
}
If I try F11 with this code, visual studio will debug the vector library files too, and I want to avoid that. I just want to follow the flow of the code inside my main();
Hi as already mentioned in my comment in VS2010 the only way to avoid stepping into STL code is to modify a registry key, as described in this post.
With VS2012 there is another way by using Visualizers.
You cannot go into external code (unless maybe showing it as assembly).
You should use F10 to step to the next instead of going inside such a function.
You also can use Shift + F11 to return to the next line (after the current function), if you are inside such external function code.