Link to home
Start Free TrialLog in
Avatar of cjm20
cjm20Flag for United States of America

asked on

VC++ app built in VS2012 crashes when run for the first time, then doesn't crash on subsequent runs

I have a VC++ application that I had been compiling in Visual Studio 2012 on a Windows 7 32-bit machine. The application (.exe) would run fine on whatever machine I tried to run it on.   However, I've run into a problem recently.  I've ported the code over to a Windows 7 64-bit machine, on which I also have Visual Studio 2012 installed.  Ever since I changed machines (from 32-bit to 64-bit), I've noticed something strange (and very irritating).  After compiling the .exe file, when I launch this .exe for the FIRST TIME on some other machine, the .exe very often will crash on this FIRST RUN (launch), either immediately after launching the application itself, or when opening an input file just after launching the program.  However, when I re-launch this .exe, no more crash.  I can generally launch this .exe many many times and still no crash.  It's as if something on the machine needed to be initialized by the first program launch.  So that after that first program launch (and crash), the .exe is then able to run fine because whatever needed to be initialized HAS INDEED BEEN INITIALIZED (this is just a guess, of course).

This is very frustrating.

There is no message box with any meaningful detail accompanying the crash.  Instead, there's just a windows dialog saying "Program XYZ has stopped working.   A problem caused the program to stop working correctly.  Windows will close the program and notify you if a solution is available."  

This is a tough bug to fix.  any help would be GREATLY APPRECIATED!!!!!

Thanks very much.
Avatar of jkr
jkr
Flag of Germany image

What do you get when you try the 1st run under Dependency Walker's (www.dependencywalker.com) 'Profile' mode (be sure to install the 32bit version for that test)?
Avatar of cjm20

ASKER

good suggestion. forgot about dependency walker.  I'll give it a shot and letcha know how it turns out.  Thank you.
Good luck! ;o)
did you check event viewer? is the crash only with release version?

if it happens with debug version as well, you could run the program from visual studio and the debugger should have a user break when crashing.

if it is in release version only you might add trace logs into functions to narrow down the location where the crash happened. the logs should open a log file in append mode, write the log, and close the file after each log. that way you can see after a crash which function was entered but not left.

Sara
Avatar of cjm20

ASKER

JKR, i gave dependency walker a shot: it's reporting the following errors and warnings:

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

Depedency Walker also gives the following error message: "Error opening file. The system cannot find the file specified (2):" on the following list of DLLs:

API-MS-WIN-APPMODEL-RUNTIME-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ERROR-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ROBUFFER-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL
API-MS-WIN-SHCORE-SCALING-L1-1-1.DLL
DCOMP.DLL
IESHIMS.DLL

Having said all this, I'm not sure if the above DLLs and errors are actually the issue. The main reason I say this is that when I run Dependency Walker on the "good" .exe (that being the .exe I built on the Windows 7 32-bit machine), I GET THE EXACT SAME LIST OF ERRORS AND WARNING, VERBATIM.  And the "good" .exe does NOT crash.

I've seen some posts online regarding the way Microsoft has changed the code within the App class's member function AddToRecentFileList(....).  I am calling this function in my code as follows:

AfxGetApp()->AddToRecentFileList(pszPath);

where pszPath is defined as follows:

const char* const pszPath;
Avatar of cjm20

ASKER

Sara, I haven't tried the event viewer approach but will give it a shot.  As for debug versus release mode, the crash is much more repeatable in release mode than debug mode.  However, I HAVE been able to get it to crash in debug mode. when I did, the call stack took me to AfxGetApp()->AddToRecentFileList(pszPath) (which I just referenced above in the post back to JKR).  I should have made a better effort at that time (of the crash) to investigate the circumstances, but i didn't, so now I just working from memory....not good :) ....
AfxGetApp()->AddToRecentFileList(pszPath);
 where pszPath is defined as follows:
 const char* const pszPath;
if pszPath is not initialized you should do it.

why are you using a const pointer pointing to a const char? it is not possible to assign a valid path to the variable beside of casting to non-const.

can you post the whole code around the statement?

Sara
Avatar of cjm20

ASKER

To be honest, I've inherited this legacy code. So, the previous programmer (for whatever reason) apparently thought it was a good idea to use const char* const.  Here are the code basics surrounding the call to AfxGetApp()->AddToRecentFileList(pszPath):

OnFileOpen()
{
CString strFileNameAndPath = "";
CFileDialog dlg;
dlg.DoModal();

strFileNameAndPath = dlg.GetPathName();

OpenFile(strFileNameAndPath);

}


BOOL CMainFrame::OpenFile(const *char const pszPath)
{

ifstreamex strFile(pszPath);

//do some file reading with the strFile object

//close the file reading object
strFile.close();

//add the file name to the list of recently opened files
AfxGetApp()->AddToRecentFileList(pszPath);

}
Avatar of cjm20

ASKER

OK, I was able to fetch some info regarding the crash (when running in RELEASE mode), as follows:

When the crash occurs, I get a windows dialog with the following info:

"An unhandled win32 exception occurred in MyApplication.exe [2844]."

This windows dialog gave me a chance to use VS2012 to debug.  So I proceeded....

The VS2012 debugger opened, and I got the following Visual Studio message box:

"Unhandled exception at 0x00B77ABB in MyApplication.exe: 0xC00000005: Access violation reading location 0x00000288."

This looks like a fairly typical array out of bounds issue....

In VS2012's output window, the following LONG list was shown:

'MyApplication.exe' (Win32): Loaded 'C:\Program Files (x86)\MyApplicationFolder\MyApplication.exe'. Module was built without symbols.
'MyApplicationr.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'MyApplicationr.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\glu32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\opengl32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\lpk.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\usp10.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ddraw.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dciman32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\setupapi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\devobj.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wsock32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ws2_32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\nsi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wininet.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-downlevel-user32-l1-1-0.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-downlevel-shlwapi-l1-1-0.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-downlevel-version-l1-1-0.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-downlevel-normaliz-l1-1-0.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\normaliz.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\iertutil.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-downlevel-advapi32-l1-1-0.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\userenv.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\profapi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msimg32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winspool.drv'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.18201_none_ec80f00e8593ece5\comctl32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oledlg.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.18455_none_72d576ad8665e853\GdiPlus.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleacc.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\nvinit.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\PGPmapih.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\PGPhk.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_41e6975e2bd6f2b2\comctl32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WindowsCodecs.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntmarta.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\Wldap32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\IPHLPAPI.DLL'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winnsi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dhcpcsvc.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\clbcatq.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wbem\wbemprox.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wbemcomn.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptsp.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rsaenh.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\RpcRtRemote.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wbem\wbemsvc.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\wbem\fastprox.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdsapi.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\comdlg32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\propsys.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\NV\ig7icd32.dll'. Cannot find or open the PDB file.
'MyApplication.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ig7icd32.dll'. Cannot find or open the PDB file.
The thread 0x111c has exited with code 0 (0x0).
The thread 0x868 has exited with code 0 (0x0).
The thread 0x1124 has exited with code 0 (0x0).
The thread 0xc8c has exited with code 0 (0x0).
Unhandled exception at 0x00B77ABB in MyApplication.exe: 0xC0000005: Access violation reading location 0x00000288.

And, the Call Stack looks like this:

 MyApplication.exe!00b77abb()      Unknown
 [Frames below may be incorrect and/or missing, no symbols loaded for MyApplication.exe]      
 MyApplication.exe!00b51970()      Unknown
 MyApplication.exe!00c32467()      Unknown
 MyApplication.exe!00c336f7()      Unknown
 MyApplication.exe!00c2ee55()      Unknown
 MyApplication.exe!00c2f5b7()      Unknown
 user32.dll!750762fa()      Unknown
 user32.dll!75076d3a()      Unknown
 user32.dll!75076ce9()      Unknown
 user32.dll!75080d27()      Unknown
 user32.dll!75080d4d()      Unknown
 opengl32.dll!6ebf60fb()      Unknown
 user32.dll!750762fa()      Unknown
 user32.dll!75076d3a()      Unknown
 user32.dll!75076ce9()      Unknown
 user32.dll!750777c4()      Unknown
 MyApplication.exe!00c3bc1e()      Unknown
 MyApplication.exe!00c3bc8c()      Unknown
 MyApplication.exe!00c3c293()      Unknown
 MyApplication.exe!00c3c450()      Unknown
 MyApplication.exe!00d62dcc()      Unknown
 FMyApplication.exe!00d62ef4()      Unknown
 kernel32.dll!7528338a()      Unknown
 ntdll.dll!77439f72()      Unknown
>ntdll.dll!77439f45()      Unknown

So, that's all the detail I've got to go on at the moment.  I'm currently combing my code looking for any arrays which I might be accessing prior to setting the size, etc.  

But please don't hesitate to offer up any advice.....
>>"Unhandled exception at 0x00B77ABB in MyApplication.exe: 0xC00000005: Access
>>violation reading location 0x00000288."

The read address indeed looks completely off. Can you try tho set the linker set to generate a map file when building your executable? Wiuth that map file, you can locate the faulting code like this: Consider e.g.

#include <windows.h>
#include <stddef.h>
#include <stdio.h>

LONG
WINAPI
ExceptionHandler(LPEXCEPTION_POINTERS pe) {

    char acModule[MAX_PATH];

    MEMORY_BASIC_INFORMATION mbi;
    HMODULE hMod;

    VirtualQuery (pe->ExceptionRecord->ExceptionAddress,&mbi,sizeof(mbi));

    ptrdiff_t RVA = (char*)pe->ExceptionRecord->ExceptionAddress - (char*)mbi.AllocationBase;

    hMod = (HMODULE) mbi.AllocationBase;

    GetModuleFileName(hMod,acModule,sizeof (acModule));

    printf( "Detected Exception in %s at RVA 0x%08X\n", acModule, RVA);

    return EXCEPTION_EXECUTE_HANDLER;
}

void FaultingFunction () {

    LONG* p = NULL;
    *p = 42;
}

void main(){

    SetUnhandledExceptionFilter (ExceptionHandler);
    FaultingFunction ();
}

Open in new window


(compiled with "cl rvaxcept.cpp /link /map")

which prints

Detected Exception in C:\tmp\cc\rvaxcept.exe at RVA 0x00001088

The map file is

rvaxcept

[time stamp]

Preferred load address is 00400000

Start         Length     Name                   Class
0001:00000000 00004938H .text                   CODE
[...]

 Address         Publics by Value              Rva+Base     Lib:Object

0001:00000000       _ExceptionHandler@4        00401000 f   rvaxcept.obj
0001:0000007a       _FaultingFunction          0040107a f   rvaxcept.obj
0001:00000092       _main                      00401092 f   rvaxcept.obj
0001:000000a7       _printf                    004010a7 f   LIBC:printf.obj
0001:000000d8       _mainCRTStartup            004010d8 f   LIBC:crt0.obj
0001:000001b7       __amsg_exit                004011b7 f   LIBC:crt0.obj
[...]

You can see the 'Rva+Base' column, base is given as 'Preferred load address is 00400000'

Add that to the faulting module RVA of 0x00001088 and you get 0x00401088, then look that up in the above (i.e. the 'nearest symbol' and you can see that it is 'FaultingFunction'

0001:0000007a       _FaultingFunction          0040107a f   rvaxcept.obj
0001:00000092       _main                      00401092 f   rvaxcept.obj

The address is between main and FaultingFunction, which starts before 0x00401088, the next function is main and starts later.

(BTW, the output of the exception handler is nothing els but the debugger's stack addresses)
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cjm20

ASKER

>>it is either a non-initialized pointer. or it is an offset (relative address) address within a class or struct where the pointer to structure is a null pointer.

Bingo!  I was accessing a null pointer to a dialog, in an openGL drawing routine.  Because the drawing code gets called a lot (using timers and due to various user actions), this null pointer was being accessed many times over, including when the openGL window is initially drawn at program startup.  I've now installed a filter to ensure the pointer is != NULL before accessing the pointer itself.  PROBLEM SOLVED.  Thanks a million Sara (and Jkr)!!!!

One thing that's strange to me is that this is legacy code. Thus, this null pointer has been accessed for many years, and doesn't seem to have been causing issues (at least not a crash immediately after the application is launched for the first time).  This bug has only been "noticeable" since I starting compiling the code on a Windows 7 64-bit machine (VS2012).  When I was still compiling on a Windows 7 32-bit machine (VS2012), the bug, while still present in the source code, didn't cause issues (that I noticed, anyway).
It will be helpful for you - and all of us here - to check out in debug mode what is happening in both platforms (32 - 64 bit). There should be the same null pointer accessed in both cases so there should be the same error in both cases. Maybe Sara solved the issue but it looks strange. Are you sure there isn't any - kind of - default settings read from somewhere?
When I was still compiling on a Windows 7 32-bit machine (VS2012), the bug, while still present in the source code, didn't cause issues (that I noticed, anyway).
I remember a few debug sessions where I stepped into a member function and the 'this' pointer was NULL. that did not crash as long as the function was read-only and did not dereference a pointer somehow.

in a 64 bit windows, it might be different when the "wrong" address points to protected memory  or when the mapping from 32-bit virtual address to 64-bit physical address doesn't accept the pointer value and throws an exception. note, access violation is not a c++ exception but an SEH exception issued by the operation system.

Sara
That's the reason I asked for verification....
It will be good to know if something like that is happening for future knowledge.