Access Violation after declaration second objet of the same type
Zarathustra
adam.chrapkowski at gmail.com
Tue Oct 20 03:59:05 PDT 2009
Sorry for long code, but I can't find the cause the problem.
// module window
module window;
private import base;
private import structs;
private static import user32;
private static import kernel32;
private static import gdi32;
private:
extern (Windows) dword wndProc(ptr o_hwnd, dword o_msg, dword o_wparam, dword o_lparam){
alias user32.EWindowMessage WM;
Window* l_wnd = (cast(Window*)user32.getWindowLong(o_hwnd, 0x00));
if(o_msg == WM.NCCREATE){
user32.setWindowLong(o_hwnd, 0x00, *(cast(dword*)o_lparam));
}
else if(l_wnd is null){
return 0x00;
}
return (cast(Window*)user32.getWindowLong(o_hwnd, 0x00)).wndProc(o_hwnd, o_msg, o_wparam, o_lparam);
}
extern void d_assert_msg(dword, void*, dword, void*);
public:
class Window{
private const ptr handle;
private static WINWndClassEx wndClass;
void onMouseDown(MouseEventArgs o_mea){
user32.messageBox(null, cast(wstr)"INSIDE", cast(wstr)"msg", 0x00);
}
static this(){
wndClass.size = 0x00000030;
wndClass.style = 0x00000003;
wndClass.wndProc = cast(ptr)&.wndProc;
wndClass.clsExtraBytes = 0x00000000;
wndClass.wndExtraBytes = 0x00000004;
wndClass.hInstance = kernel32.getModuleHandle(null);
wndClass.hIcon = user32.loadIcon(null, 0x00007F00);
wndClass.hCursor = user32.loadCursor(null, 0x00007F00);
wndClass.hbrBackground = gdi32.getStockObject(0x00000000);
wndClass.menuName = null;
wndClass.className = cast(wstr)"clsname";
wndClass.hIconSm = user32.loadIcon(null, 0x00007F00);
if(!user32.registerClassEx(cast(ptr)&wndClass)){
user32.messageBox(null, user32.translateErrorCode(kernel32.getLastError()), cast(wstr)"error", 0x00000000);
assert(false, "window class registering failed");
}
}
this(){
handle = user32.createWindowEx(
0,
cast(wstr)"clsname",
cast(wstr)"<applicationcaption>",
0x00CF0000,
0x00000000,
0x00000000,
0x00000280,
0x000001E0,
null,
null,
kernel32.getModuleHandle(null),
cast(ptr)&this
);
if(handle is null){
user32.messageBox(null, user32.translateErrorCode(kernel32.getLastError()), cast(wstr)"error", 0x00000000);
assert(false, "window creating failed");
}
}
public void run(){
WINMsg msg;
user32.showWindow(handle, 0x0000000A);
user32.updateWindow(handle);
while(user32.getMessage(cast(ptr)&msg, null, 0, 0)){
user32.translateMessage(cast(ptr)&msg);
user32.dispatchMessage(cast(ptr)&msg);
}
}
private dword wndProc(ptr o_hwnd, dword o_msg, dword o_wparam, dword o_lparam){
alias user32.EWindowMessage WM;
alias user32.EMouseKey WK;
switch(o_msg){
case WM.DESTROY:
user32.postQuitMessage(0x00);
break;
case WM.LBUTTONDOWN:
MouseEventArgs l_mea;
l_mea.button = MouseButton.LEFT;
l_mea.location.x = loword(o_lparam);
l_mea.location.y = hiword(o_lparam);
user32.messageBox(null, cast(wstr)"BEFORE", cast(wstr)"msg", 0x00);
onMouseDown(l_mea);
break;
default: return user32.defWindowProc(o_hwnd, o_msg, o_wparam, o_lparam);
}
return 0;
}
}
/* the following works correctly when wnd1.onMouseDown */
// main1
void main(){
Window wnd1 = new Window();
Window wnd2;
wnd1.run();
}
/* the following fails when wnd1.onMouseDown */
// main2
void main(){
Window wnd1 = new Window();
Window wnd2 = new Window(); // allocator
wnd1.run();
}
Inside ctor of Window class I don't see nothig wrog!
More information about the Digitalmars-d
mailing list