Access violation after inheriting.

Zarathustra adam.chrapkowski at gmail.com
Mon Oct 19 00:10:58 PDT 2009


Could somebody explain memory rules in class inheriting?
The following code illustrates my problem, look at 'main' function:
//____________________________________________
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;
  auto 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);
}

public:
class Window{
  private const ptr handle;

  void onMouseDown (MouseEventArgs o_mea){
  }

  this(){
    WINWndClassEx wndClass;

    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)){
      if(kernel32.getLastError() != 0x0582){
        user32.messageBox(null, user32.translateErrorCode(kernel32.getLastError()), cast(wstr)"error", 0x00000000);
        assert(false, "window class registering failed");
      }
    }

    handle = user32.createWindowEx(
      0,
      wndClass.className,
      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.x = hiword(o_lparam);
        onMouseDown(l_mea);
      break;

      case WM.RBUTTONDOWN:
        MouseEventArgs l_mea;
        l_mea.button = MouseButton.RIGHT;
        l_mea.location.x = loword(o_lparam);
        l_mea.location.x = hiword(o_lparam);
        onMouseDown(l_mea);
      break;
      
      default: return user32.defWindowProc(o_hwnd, o_msg, o_wparam, o_lparam);
    }
    return 0;
  }
}
//____________________________________________
module main:
// ...
class MyWindow : Window{
  this(){
    super();
  }

  override void onMouseDown(MouseEventArgs o_mea){
    MsgBox(cast(char[])"Hello");
  }
  
}

void main(){
  
  try{
    Window   wnd   = new   Window();
    MyWindow mywnd = new MyWindow();
    wnd  .run();  // ok
    mywnd.run();  // access violation onClick
  }
  catch(Object o){
    MsgBox(cast(char[])o.toString);
  }
}
//____________________________________________



More information about the Digitalmars-d mailing list