F11 Debug line by line only my .cpp? - c++

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.

Related

Visual Studio Code problem with C++ 20 Coroutine parts - suspend_never, suspend_always, co_await

I'm trying to switch from JetBrains CLion to VS Code. When I was trying some code that worked in CLion perfectly, I ran into troubles. I was trying to open only a basic coroutine implementation.
Line:
#include <coroutine>
is good, without errors, but when I try something like:
std::suspend_always initial_suspend()...
or:
co_await std::suspend_never() {}
VS Code shows problems that "namespace std contains no member suspend_always", etc.
Like I said- the only thing I did was that I opened the same code, that worked in CLion.
The first thing I tried was checking paths to compiler (mingw gcc) - the same as in CLion.
Then I tried to add the line
using namespace std;
, but it helped only on one line. I don't get it.
I tried also change standard in cpp_properties.json file to "C++20".
Nothing on google helped me, but maybe I just donĀ“t know what to search for.

Random error in exe_common.inl in Debug build

I am using VS 2022 Community Edition (v17.3.3) to build wxWidgets application (v3.2.0) using C++ (v14.3 - Features from Latest C++). The windows SDK is using the latest installed (10.0.22621). The project is also using C++ modules.
The Debug build succeeds but when I run the project's exe file at random it throws the exception (Access violation reading 0xFFFFFF (ucrtbased.dll)) in exe_common.inl at the following line:
__scrt_current_native_startup_state = __scrt_native_startup_state::initialized;
After a few more compilations (by just making minor changes to trigger a compilation) it succeeds and the exe runs correctly.
I wonder if there is any settings that might be causing this random error. Btw, I am using Win11 but same thing happens on Win10 as well.
Thanks in advance.
EDIT 1:
The project is using boost libraries and at startup boost/json (boost/json is used in other parts of the project as well). Debugger shows that after the following line the above error happens:
static allocator_arg_t allocator_arg = BOOST_CONTAINER_DOC1ST(unspecified, *std_allocator_arg_holder<>::dummy);
There were a few things needed attention:
Discontinued use of wxSQLite (the library was not maintained for over a decade),
The main frame was a singleton data structure, not anymore, and not deriving from wxMDIFrame anymore.
All unnecessary (a chain of them) #include removed.
Inclusion of <boost/json.hpp> in a few files were removed and now using #include <boost/json/src.hpp> only in one .cpp file. However, the project still uses inclusion of <boost/json/value.hpp> in multiple .h files.
All uninitialized pointer variables and others were initialized.
#1 and #4 were especially pointed by the debugger. It has now been more than a few days and haven't had the problem since then.

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".

In Visual Studio C++, How to quickly find necessary header files?

I know a fair amount of Java and Eclipse IDE, but am new to Visual Studio and C++. In Eclipse/Java, if you use a predefined class, Eclipse helpfully suggests the appropriate header file to include for the code to compile. Wondering if Visual Studio has similar functionality.
For example every time I use a code sample from the web, I spend a lot of time Googling which header files to include so the code will compile. My current challenge: I'm writing a small utility that reads filenames in a directory into an array for batch renaming. For this, I'm using following code fragment:
DIR* dir;
struct dirent* dirEntry;
dirEntry = readdir(dir);
Visual Studio is giving the error message: "DIR" is unidentified. "readdir" is unidentified.Is there an efficient way to locate the appropriate header files for C++ code fragments to resolve error messages like these? Thanks.
I tested this feature in vs2019 community 16.3.6 and it works. When you hover the mouse at the location of an error, you can see an error light bulb. And click the drop-down arrow next to the error bulb to add missing #include.
You can also press Alt+Enter.
As mentioned in some answers above, Visual Studio has started offering some suggestions for header files. But as of this writing, some VS suggestions lead to other error messages. E.g. I just used getline(). VS gave error message: Identifier "getline" is unidentified. It suggested I add using namespace std::basic_istream; to my code. But this was not applicable to my code and produced additional error messages.
After stumbling around, I found a very simple solution: Visit the C++ reference website. There I searched for getline and found the header information at the following link: getline(). To fix the error, I needed to #include <string>.

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

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.