Random error in exe_common.inl in Debug build - c++

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.

Related

C++ Program will not run due to Windows DLL issue

This code will run on your machine, but not on mine:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> vv;
vv.push_back(1); //<--- without this, everything works
cout << "X\n";
return 0;
}
The reason, is that it appears my windows machine is missing a (or has a conflicting) specific DLL related to Vectors which is causing my other projects to crash without a report if I use vectors.
However, what's even more entertaining is:
If I comment the push back line, it executes normally without a crash.
Other programs work completely fine as long as they avoid vector, as in using list or an array.
GDB Reports "During startup program exited with code 0xc0000139." Which means it couldn't find the dll it wants. Looking through the dependencies of my executable with Dependency Walker is rather inconclusive, I can't tell what is wrongly missing and what is normally missing among the dlls.
It could also be that there is a second dll that is conflicting with it (like I downloaded two different compilers). Investigating with Process Monitor informs me that instead of using the MinGw64 files located in the MSYS2 folder, the process uses the dlls inside of my ProgramFiles/Git/mingw64/bin location.
So it could be an issue? But Process Monitor doesn't tell me why or what is causing the process to exit unsuccessfully...
How would I get Windows to use the dlls in MSYS2 instead of the Git location? The solution that the post I linked above says is to remove the "erroneous dlls." But I can't just remove dlls that git uses?
Also, how am I guaranteed that switching to the dlls within MSYS2 is going to fix my problem? I just want to use vectors lol, how hard does this have to be?
Other solutions include leaving this spyware infested OS and enter the world of Linux, but that's a huge change.

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

Unable to load library issue with external dependencies

I have created a C++ *.dll in VS 2013. This runs fine on my development machine, however is throwing an error when run on a clients machine.
Here is the cpp file.
#include "stdafx.h"
#include "EasyDLL.h"
using namespace System;
double WINEXPORT EASYDLL_GetServerDateTime(int clientServerTimeDiff)
{
System::DateTime clientDateTime = System::DateTime::Now;
System::TimeSpan duration( 0, 0, clientServerTimeDiff );
System::DateTime serverDateTime = clientDateTime.Add( duration );
String^ strTemp = serverDateTime.ToString("yyyyMMddHHmmss");
double result = double::Parse(strTemp);
return result;
}
I believe the reason is, the external dependencies don't exist on the client's machine.
I don't have much experience with C++.
Lead up to question:
When you compile (and I'm just compiling in debug at the moment), there is a whole bunch of header files in the external dependencies and a dll (mscorlib.dll).
The mscorlib.dll exists in the same place on the client machine, so I'm kind of discounting that as the problem, but the headers do not exist.
Question:
Do I need to ship these headers off with the dll I've created or should they be compiled into my dll?
Here's a screenshot of some of the external dependencies. Most of them seem to be in the project by default when the project was created.
EDIT:
After changing the project to release it seems to be running on another machine as well as my dev machine, have to wait untill tomorrow to test on the client's.
Although it has worked so far, I checked out DependencyWalker and got the following when looking at my dll, is this anything to be worried about?
You don't need to ship the headers with the DLL. If Mats answer doesn't solve it you might want to try using this tool: http://www.dependencywalker.com/
You can "look inside" a DLL that way. From my experience, best run it on both machines i.e. on the dev machine and the client machine because what might look like an error on the client machine, may also exist on the dev machine, thus discounting it as the problem.
Good luck!

C++ Error RC2104 trying to compile PuTTY-PSCP (for Windows) on Visual Studio 6.0

I was about to use PuTTY Development source code for Windows to create my own client application (found here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) but as I tried to compile the PSCP project (SCP Client), I got the following error :
C:\work\2015\Putty\windows\version.rc2 (18): error RC2104 : undefined
keyword or key name: BINARY_VERSION
I've been going through the various posts involving this error but didn't find anything working :
error RC2104: undefined keyword or key name: DS_SETFONT :
On this post I noticed that the version of MSVC was brought up so I figured maybe something has to be done to get PuTTY to work on VC 6.0 ?
Also I tried to add #include <windows.h> in both version.rc2 (version.rc2 is used for inclusion in all .rc files) and pscp.rc, none worked.
I'll be quick to answer if you need any information (project properties, source code...)
USING Visual Studio 6.0 with SP6 on Windows 8.1
Probably, wrong version.h is seen.
Correctly, the file version.h in the project folder should be seen.
Please try to modify version.rc2:
#include "version.h"
to
#include "..\\..\\..\\version.h"
At least, resource compiler will end successfully.
If you search through the PuTTY source files, you'll notice that BINARY_VERSION is defined in version.h and used in windows/version.rc2, which #includes version.h.
Since your version.rc2 isn't seeing version.h, try to figure out why: Is version.h still present and does it still contain BINARY_VERSION? Are your include paths correct? Is there another version.h somewhere else in your include path that's getting picked up by mistake?
Which source code are you using ?
I tested latest(0.64) "Release source code for Windows".
direct link is
http://the.earth.li/~sgtatham/putty/latest/putty-src.zip
I had tried to compile using VC++ 6.0 Professional with SP6, on my PC,
running Windows XP SP3.
After extracting putty-src.zip to somewhere with keeping folder
structures, did you correctly opened 'putty.dsw' in "putty-src\windows\MSVC" folder?
You should be find in 7 projects in 'FileView' tab of the workspace
in Visual Studio 6.0.
You can switch active project to 'pscp' with context menu via
right button click on 'pscp' project.
With modified version.rc2, resource compiler finished successful.
But two (sshshare.c, winsftp.c) C source files failed compiling
with 20 errors. in 'pscp' project.
Errors while compiling 'winsftp.c' is caused 'TIME_POSIX_TO_WIN'
and 'TIME_WIN_TO_POSIX' macros.
'ull(unsigned long long)' is a 64-bit integer-suffix, newly defined in C99. Since C99 standard is not support on VC6, then caused errors.
I had temporally modified
11644473600ull ------> ((ULONGLONG)11644473600)
10000000ull ---------> ((ULONGLONG)10000000)
and confirmed errors are cleared. (Sorry, no validation the code is correctly generated)
3 errors while compiling 'sshshare.c' is also caused another macro.
I cannot understand why you got 116 errors.

Can not find mpirxx.h after building the mpir-2.6.0 library in vc++

I need to use mpir-2.6.0 library with visual c++ 2010. My code is going to be in c++.
I extracted both folders (mpir-2.6.0 and vsyasm-1.2.0-win32). Then, I copied the content of the folder vsyasm-1.2.0-win32 (including vsyasm.exe after renaming it to yasm.exe and placing it in: C:\Program Files\Microsoft Visual Studio 11.0\VC\bin\. Then, I opened the project: mpir.sln which is placed in: \mpir-2.6.0\build.vc10. Then, I changed the Project configuration to: Release.
When I try to build the whole solution, I get a lot of errors such as:
error C1020: unexpected #endif c:\proj\mpir-2.6.0\mpir-2.6.0\mpir.h 4 1 lib_mpir_gc
error C1020: unexpected #endif c:\proj\mpir-2.6.0\mpir-2.6.0\mpir.h 4 1 lib_mpir_gc
error C1020: unexpected #endif c:\proj\mpir-2.6.0\mpir-2.6.0\mpir.h 4 1 lib_mpir_gc
But when I build every library separately, it is successful.
After the build, I went to: \mpir-2.6.0\build.vc10\Win32\Release
and find the files:
mpir.lib
mpirxx.lib
mpir.pdb
mpirxx.pdb
But I can not find the files:
mpir.h
mpirxx.h
Which I need to copy them the visual studio include file.
My main source for these configuration is: http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/ (but this was for old versions for the library and the ysam). I do not understand the reasons for these errors. I was able to configure the library with old version. But I need to upgrade as there are improvements in the library and mine is very old one.
The output files are now located in mpir\lib\Win32\Release or mpir\dll\Win32\Release.
The recommended way to build MPIR specifcally for your CPU is to first run mpir_config.py which is located in the build.vc10 directory. mpir_config.py will prompt you with a list of CPU options. Then when you open mpir.sln, you should only compile code for the specific CPU. And then you compile the C++ wrapper (i.e. compile lib_mpir_p3 first, and then lib_mpir_cxx).
Edit: I just noticed another error from your original post. You need to rename the directory "mpir-2.6.0" to "mpir". The directory layout needs to be "<>\mpir\build.vc10"
I had the same problem with MPIR 2.6.0 and VS 2012. What I did to make MPIR work was to build lib_gc and lib_cxx, both with win32 and release mode. Because my c++ application is win32 console, this way it worked perfectly.
Becuase my windows is 64, I thought I had to build 64 versions, but turned out that I was wrong.