MS Visual C++ Runtime Library error on launch - any debugging tricks? - c++

When launching my app, I'm getting the below error dialog. I understand this indicates a problem loading the runtime library. The problem is, I'm not seeing any way to get more specific info. Which library? What was the exact problem it had when loading? etc.. System event viewer doesn't have any entries for it. Are there any tricks to finding out exactly which library it was trying to load when it hit the error and what the specific problem was?
Microsoft Visual C++ Runtime Library
Runtime Error!
Program: exe path
R6034
An application has made an attempt to load the C runtime library incorrectly.
Please contact the application's support team for more information.*
My current approach for dealing with runtime lib dependencies is to scan the install directory, extracting out the embedded manifests for the .dll and .exe files and then generating .config and .2.config files to re-direct to the minor versions of the runtime lib I'm shipping with. In the past this has always gotten things working. Not so in this case.
This might be complicated somewhat by the fact that the calling code is native C++ but some of the DLLs use .NET. I'm calling a C++ API, but under the hood it ends up utilizing some DLLs which themselves use .NET.

Use SysInternals Process Monitor to monitor file system access. Filter on process name and operation (CreateFile) to see what DLLs the process can't locate.

It could indicate a wrong version of msvcr*.dll.
You could try Dependency Walker (http://www.dependencywalker.com/) to find out where it's being linked.

Make sure you have a debug version of the code you're trying to attach to. On the Debug menu, select Attach to Process. Use processID -
Also check this -http://support.microsoft.com/kb/235434
Resolution 1
You should be able to attach to the process using Task Manager:
Enable just-in-time debugging (JIT):
In Visual C++, on the Tools menu, click Options.
On the Debug tab, make sure that Just-in-time debugging is selected.
Run Task Manager and select the process to attach to. Right-click and select Debug.
Resolution 2
Run Task Manager and get the process ID for the process you want to debug.
At a command prompt enter the following:msdev -p
This will start Visual C++ and will attach to the process specified.

Related

How to set up Visual Studio to debug a DLL ("Unable to start program error")

Basically, I have a Visual Studio project that builds a DLL (a VST audio plugin). Where this type of project scenario has been set up for me in the past, I would be able to build, run, and debug the plugin. Visual studio would automatically launch whichever program I was using to host the plugin. I am trying to achieve the same effect in my current project, but I don't know how to set that up. Currently when I build and run my DLL in Visual Studio, I get the error "Unable to start program". The DLL still builds, and I can still run it, but I can't debug it from Visual Studio, because I don't know what I need to do in my project settings to make this happen. How can I do this?
MORE INFO:
What I do know is that, in projects where this sucessfully works, there are some modifications made to the Visual Studio project settings under fields marked 'pre-build events' and 'post-build events', so presumably what I want to do is edit these in some way to tell Visual Studio the following: "Hey, before you try and run and debug this DLL, you have to launch another program (my program is called Max.exe), and then you have to wait until that program loads the DLL. Then you can debug! Don't be a stupid computer and try to debug it before it's even loaded in Max.exe..."
What I do not know : EVERYTHING ELSE. This is literally all I know about what I'm trying to do, hense the colourful attempt to talk to a computer in English.
Currently when I build and run my DLL in Visual Studio, I get the error "Unable to start program". This is unsurprising seeing as the project knows nothing about the environment I want to use to test the DLL, but the problem is that I don't have a clue what Visual Studio needs to know. I really don't know enough about programming to understand the implications of what I'm trying to do either. Yes, I did mention those fields marked pre-build and post-build because I remember them being important, but I don't know exactly what or how to write in those fields, and I also do not know if there will be more things I need to tell Visual Studio before this will work.
Q.E.D I'm not actually sure what pre and post build events are, or how they work. And I barely know the first thing about customizing VS project settings. All I know is how to write audio processing code. I felt the need for this disclaimer because typically my questions are met with angry programmers who think I don't do my own research; they fail to realize I am an audio engineer who skipped programming 101. Yes, how to debug a dll is a common question I'm sure, but answers to those questions tend to assume pre-requisite knowledge that I do not have.
You will want to edit the Command field in your project's Debugging properties. Right-click on your project in the solution explorer and click Properties (it's generally the last item). Open the Debugging page under Configuration Properties. The Command field indicates which executable to launch when debugging.
By default this contains $(TargetPath) which refers to the final binary your project compiles. This is useless for DLLs since DLLs are not executable. Change this to the path of whatever third party application you are writing a plugin for.
With this change, launching with debugging will actually launch the third party application and attach the debugger to it. Once the application loads your plugin, you will be able to debug it normally.
For Visual Studio,
In Solution Explorer, right click on project and select Properties.
In Properties, choose Configuration Properties -> Debugging.
For Command, enter the full path of the executable that will be loading your DLL. Fill in the Command Arguments and Working Directory accordingly.
In addition, you need to make sure that the executable actually loads the DLL you are building. A mistake that a lot make is to launch their executable, and not realize the executable is loading another version of the DLL they are trying to debug. This can happen due to Windows searching for the first DLL that it finds using the DLL searching logic (exe directory, path, etc.).

Dumping a stack trace from C++ (Windows) - fails to find symbols

I'm a developer of Windows desktop software, and from time to time our app crashes. In rare cases I'd like to get a customer to run a debug version of the app to send me a stack trace so I know where it crashed. I followed the instructions in here:
Windows C++ stack trace from a running app
...but while it works on my development machine, it doesn't work on any client machine or those of my colleagues, who don't have Visual Studio installed. So I presume that there's some .dll or something they need before it'll work. They're using the same .exe I'm using, i.e. the one I compiled in VC++ in debug mode.
After some painstaking "message window" debugging, I learnt it's failing in SymGetSymFromAddr64() - this returns FALSE. But when I walk the stack, this always returns FALSE or it returns garbage that doesn't make sense (random unrelated method names), as if it's the PC values which are invalid, not the mapping process. To reiterate, it's a debug mode .exe that produces a perfect symbolic stack trace on my development machine.
I did some research and found some mentions of "dbghelp.dll" and "imagehlp.dll" but I just ended up confused. "dbghelp.dll" ships with all versions of Windows, but with reduced functionality. There's some other things I could install, but it's a little scary to be installing some Windows "WDK" or "debug kits" which might overwrite important system .dll's or do god-knows-what to your computer.
So what I need to know is: "what's the simplest set of instructions I can give to these helper customers e.g. the minimum set of .dll's and where to stick them so that we can get proper symbolic information out of the stack traces when our program crashes?"
The most likely reason for failing to find the symbols is that the .pdb file cannot be found. Even if you generate a .exe in debug mode, the symbols are not in the .exe, they are in the .pdb file. Through a lot of empirical testing, it seems that the process has the pathname of the .pdb hardcoded in it, so if your clients don't have that file at that location, they won't necessarily find it. However, you can supply a "search path" to the SymInitialize() function - a folder or set of folders to use to search for the .pdb file.
In my configuration, I had an exe called "Edval.exe" and a .pdb called "DebugEdval.pdb". The process searches for "DebugEdval.pdb" in the search folders. This corresponds to what you've configured in "Properties > Linker > Debugging > Generate Program Database File".

C++ application crash detect on windows

One of my C++ MFC application randomly crash on some windows(7,xp) computers. This applications use some third party libraries (Apache axis) and when i check with dependency walker it shows application exit after loading a DLL related to this third party libraries.
So I need a way to detect what is the crash cause for this application. Is there any way to do this?
I read it can use .pdb files to host together with .exe file identify the crash issue, but as usual Microsoft doesn't have clear documentation how to use.
I try to use IBM purify trial version. but I doesn't show any valid information.
I tried to use WinDbg. but i couldn't find how to use it.
Any assist really appreciate.
EDIT: crash report added
Problem Event Name: APPCRASH
Application Name: installer.exe
Application Version: 5.0.1.0
Application Timestamp: 51ac0bdc
Fault Module Name: AxisXMLParserXerces.dll
Fault Module Version: 1.0.0.0
Fault Module Timestamp: 51ac0016
Exception Code: c0000005
Exception Offset: 0000161d
OS Version: 6.1.7601.2.1.0.256.4
Locale ID: 18441
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789`
Below are a few steps that you can take to try and locate the source of the problem.
Build a release version of the application. Make sure generating PDB files is enabled.
Send to client. Make sure they change their "Checking For Solutions" settings (see below).
When it crashes they should select "Show Program Details"
Have them record the "Fault Module Name", "Fault Executon Code" and "Fault Execution offset"
Start the exact same version you sent them. The executable and PDB files should be in the directory they were originally saved to when they were built. (e.g. solution\project\Release). I recommend that before you send them the files you zip them up the entire "Release" directory and stash it somewhere. This way you have an exact copy in case you accidently do a clean or other operation that changes the files.
In Visual Studio attach to the process with the debugger. (In the "Debug" menu)
Break the execution of the application. (Again, in the "Debug" menu)
Open the disassembly window. (Once again, in the "Debug" menu)
In the "Address" input line at the top of the disassembly window enter the address of the crash based on the current load address of the module and the "Execution offset" provided by the crash report. If the module is not loaded you will need to modify your application to force load it at startup (this will require you to send them a new version and start over from step 1). Make sure you check for errors as the crash may be a result of a failed DLL load.
From here you will have to wing it based on your knowledge and experience with your application and the third party libraries. At the location where the crash is expected to occur place a breakpoint. Anytime the breakpoint is hit look at the call stack to see where the call is originating from. The problem may be in your code or the third party libraries (but probably your code).
You should make an effort to reproduce the crash. Have the users record what actions they were performing when the crash happened. Also have them include any logs that the application or libraries generate.
This will increase your chances of locating the problem but as I stated above you will need to rely on your knowledge of your application and the libraries.
I recommend that before you start you add some code to your application to force a crash and run through these steps yourself so you are familiar with them. It may take a few tries before you get it right and you may need to adjust the steps slightly and tailor them to your environment and application. Remember to remove the crash code before you send it to the users.
Since this seems to be you first exposure to using the debugger prepare for some frustration. It's normal.
*Some of these steps assume you are using Visual Studio. WinDbg has a different interface but the concepts are the same.
**These steps also assume Windows 7. Windows XP shows a window containing similar information when the application crashes and does not need to be reconfigured.
.
.
.
Changing checking for solution settings
Open Control Panel
Open the Action Center
Click ""Change Action Center Settings"
Click "Problem Reporting Settings"
Select "Each time a problem occurs, ask me before checking for solutions.
You could consider getting a copy of AQtrace, which is a tool designed to handle this exact circumstance. You could also consider setting your client up with an automation tool, e.g. WinTask, to capture the steps they're using to cause the problem in the first place. This greatly simplifies reproducing and eliminating it.
CrashRpt is a good open source tool we use for MFC applications. User can see CrashSender window after a crash happened and then he can send a crash report containing minidump file that can be opened in MS Visual Studio. Of course you need to keep your binaries and corresponding .pdb files for particular build to get a correct call stack while running a crash dump in VS.
Some crashes happen in third party software as well. We're able to notify customers about such cases.

Why is my DLL failing to register?

I am building a project in VS2005 and several of my DLLs are failing to register. The error message I am getting in Visual Studio is:
Project : error PRJ0019: A tool returned an error code from "Registering ActiveX Control..."
which is nicely vague. When I register the DLL manually through the command line (using regsv32.exe, I get the following error:
LoadLibrary("test.ocx") failed - This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix the problem.
I ran Dependency Walker (depends.exe) on the culprit .ocx file but it isn't showing any obvious problems.
I have also done a re-build but I'm still getting the same problem.
Any suggestions as to how I could determine the cause of this failure to register?
Microsoft had recently released a Security Update for ATL (KB971090). It is un update on top of MSVS2005sp1 and it's both compilate-time and runtime compatibility breaker. Check if your building environment has this patch.
References:
ATL Security Update:
http://msdn.microsoft.com/en-us/visualc/ee309358.aspx
Breaking changes in ATL:
http://msdn.microsoft.com/de-de/library/ms235654.aspx
And this is a must read:
http://tedwvc.wordpress.com/2009/08/10/avoiding-problems-with-vc2005-sp1-security-update-kb971090/
Most probable is because the embedded manifests. You should take a resource explorer application and check your DLLs for the embedded manifests. It might be that one of the dependent DLLs (or your DLL) require some versions of other DLLs which don't exists.
I got this message: "This application has failed to start because the application configuration is incorrect." in case of embedded manifest mistmatches.
Probably the easiest way to troubleshoot this whole category of problem is to install Process Monitor from microsoft.com.
Process Montior lets you observe the system calls processes are making, and in this case you will be able to see if some system call is failing. For example, if you are lacking a dependency, then a CreateFile() call will be seen failing with a DLL, OCX, etc. as the filename.
Launch procmon and configure the filter to exclude events other than from regsvr32.exe, reproduce your test case, and then check the log. Look for NAME_NOT_FOUND errors in the return value column.
Do you have the C++ Redistributable Package Installed?
There are several things you can try:
try regsvr32 w/ fusion log enabled (fuslogvw.exe - it works for unmanaged dlls as well). This would give you a bit more information than depends on what external dependencies are loaded, where are they loaded from and what errors were hit.
copy the .ocx and its dependencies to the root or a first level folder and try registering from there. I don't remember details, but there was an old problem with registering a COM dll from within too deep of a path.
run regsvr32 under WinDbg. Set a breakpoint DllMain and see if it does anything funky.
If you never break on DllMain in WinDbg, set a breakpoint on module load for your dll and once it's hit, you can either step through LoadLibrary, or just set a generic load library breakpoint and check every dll after that.

Multithreaded Windows service in MingW

I am trying to build a Windows service with MingW. It need thread safe exceptions, so I added the linker flag -mthreads. The application works fine from the command-line, but when I try to start it from services.msc, the 1054 error ("The service did not respond to the start or control request in a timely fashion") is raised. The service starts if I re-build it without the -mthreads flag. How can I get this working with -mthreads?
I suspect -mthreads is bringing in a dependency on a DLL, and that DLL is not on the path when it's running as a service. In my cygwin environment, if I compile a trivial program with "-mno-cygwin -mthreads", I get a dependency on MINGWM10.DLL, which certainly wouldn't be on the path when running as a service. If I try running it with no PATH set, it crashes as it starts to load (and leaves a turd in the Application event log).
I'd be bringing up your exe in Dependency Walker (http://www.dependencywalker.com) to see what you're loading at load-time, and check your Windows Event Log to see if there are any hints there. You're probably going to need to put a copy of the DLLs it needs alongside the executable.
You need mingwm10.dll in the working directory or in [edit: system, not per user] PATH, because C++ programs compiled with -mthread option have that dependency. If you're pretty sure exception will never be thrown by your code nor propogate through your stack, use -fno-exception instead of -mthread to resolve the dependency.
I wonder if you can debug it when it runs as a service. There must be something spooking your program when service host runs it. Perhaps try to attach a debugger to svchost.exe, at least you can see what modules are loaded and maybe which exception causes the crash.
Is your application even starting up at all? Put a call to OutputDebugString (or equivalent) at the start of your main function to see if it even gets that far. (Grab DbgView from SysInternals if you don't have it already.)
If it doesn't get that far, we start checking for the obvious: is it a matter of the application not finding the runtime DLL? It could be that you have the regular runtime in its PATH, but it can't find the MT version. That could explain the behaviour you describe. You may need to copy the MT runtime or update the PATH accordingly.