Operaror 'new' inside WinMain crashes at runtime

Derek Parnell derek at nomail.afraid.org
Tue Aug 8 17:28:41 PDT 2006


On Wed, 09 Aug 2006 03:12:47 +0300, Serg Kovrov wrote:

> Consider this sample:
>> import std.c.windows.windows;
>> 
>> class Test {}
>> 
>> extern (Windows)
>> int WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow)
>> {
>> 	new Test(); // <-- here
>> 	return 0;
>> }
> 
> When running executable got a runtime error. There is some info/dump on 
> the error, but I can't save/copy it. This is part of it: Exception 
> Information - code: 0xc0000005; Flags: 0x00000000; record: 
> 0x0000000000000000; Address: 0x0000000000402be5; and some system 
> information, followed lots of modules information...
> 
> This only occurs within WinMain program. eg console
> application (with 'main()' entry) did not reproduce this error.
> 
> Compiled with: `dmd test.d gdi32.lib test.def`
> test.def is standard windows def:
>> EXETYPE NT
>> SUBSYSTEM WINDOWS
> 
> Can anyone confirm this?

Confirmed. However this is not the way to write Windows apps. You missed
out all the GC stuff. The code should look like this ...

import std.c.windows.windows;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleDtor();
extern (C) void _moduleUnitTests();

extern (Windows)
int WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int
cmdShow)
{
    int result;

    gc_init();          // initialize garbage collector
    _minit();           // initialize module constructor table

    try
    {
        _moduleCtor();      // call module constructors
        _moduleUnitTests(); // run unit tests (optional)

        result = mymain(instance, prevInstance, cmdLine, cmdShow);

        _moduleDtor();      // call module destructors
    }

    catch (Object o)        // catch any uncaught exceptions
    {
        MessageBoxA(null, cast(char *)o.toString(), "Error",
            MB_OK | MB_ICONEXCLAMATION);
        result = 0;     // failed
    }

    gc_term();          // run finalizers; terminate garbage collector
    return result;
}

class Test {}
int mymain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int
cmdShow)
{
    auto x = new Test();
    return 0;
}


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
9/08/2006 10:22:22 AM



More information about the Digitalmars-d-bugs mailing list