Static Library Containing Dialog and Resource

From: Boogie With Stu (YoHoHoHo_and_a_bottle_of_rum_at_pirate.com)
Date: 05/11/04


Date: Tue, 11 May 2004 01:56:56 GMT

I have a static library that links with the static MFC lib. In this
library I create a CDialog and display it. I'm using
InitModalIndirect() to create the dialog from a resource template.
The resource template is in a BYTE array within the library.

Everything works great with the debug build. The dialog displays and
handles messages, etc. But when the calling app that links in the
static library (a straight Win32 app in this case) exits it gives an
Access Violation. Well, this only happens in release build and not
the debug build (of course, sigh).

I double checked my run-time libs and they all match correctly. I'm
completely stumped as I can't seem to figure out what causes the
Access Violation - nothing odd shows up in the debug build. Ran the
code through BoundsChecker and still no issues (in debug). In release
BoundsChecker just tells me there was an Access Violation when the app
exits.

Here's the function in the static lib that creates the dialog:

CWinApp theApp;

//Entry Function
int ShowDlg()
{
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, "", 0))
        {
           return 0; //failed
        }
        
        if (!AfxGetApp()->InitInstance())
        {
                if(AfxGetApp()!=NULL)
                {
                        AfxGetApp()->ExitInstance();
                }
                AfxWinTerm();
                return 0; // Init Failed
        }

        //create the dialog and show it
        CDlgTempl* dlg = new CDlgTempl;
        dlg->InitModalIndirect(dlg->GetTemplate());
        dlg->DoModal();
        delete dlg;
        
        if(AfxGetApp()!=NULL)
        {
                AfxGetApp()->ExitInstance();
                return 0;
        }

        // Terminate the library before destructors are called
        AfxWinTerm();
        return 1;
}

The GetTemplate() method of CDlgTempl returns a LPCDLGTEMPLATE.

All the above code runs fine in both builds. It is when the calling
app exits with the release build I get the Access Violation. No
linker warnings exist either. Everything seems clean.

Here's the calling app:
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine,
                     int nCmdShow)
{

        return ShowDlg();

}

Any ideas??? I'm stumped. I had lots of debug code in there so I was
able to narrow down the access violation to the exit of WinMain.

-- 
Boogie With Stu


Relevant Pages

  • Re: Bug in STL VS2005 / Release Lib in Debug EXE -> Buffer overrun
    ... der Applikation sowie der Lib identisch sind, mit Ausnahme von Release und Debug. ... Eine ReleaseLib zu erstellen die in einer Debug App benutzt werden kann, ... Dafür Sorge das kein Speicher der in der Lib allokiert wird, ...
    (microsoft.public.de.vc)
  • RE: How can I fix these errors.....
    ... Exception number: c0000005 (access violation) ... fault address 0x000108df. ... Nvidia Driver 158.22 and Viewsonic Monitor drivers also seemed to ...
    (microsoft.public.windowsxp.help_and_support)
  • CreateProcess DEBUG_PROCESS fails to show any window
    ... it creates the proc with a valid thread and proc handle ... returned showing fine in Task Man but the window fails to load. ... IF I create an appication without debugging info and the target app to ... load has no debug info CreateProcess with DEBUG flag loads the process ...
    (microsoft.public.vc.debugger)
  • RE: How can I fix these errors.....
    ... Exception number: c0000005 (access violation) ... to install Necessary Hardware Drivers. ... Nvidia Driver 158.22 and Viewsonic Monitor drivers also seemed to ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Heres what Ive decided to do [Was: Should I raise a fuss over this?]
    ... or two people around that might have a requirement for a simple app such as ... Download it, 30 seconds? ... Install it maybe a minute? ... Test it, Debug it??? ...
    (microsoft.public.vb.general.discussion)