Access violation after inheriting.
Daniel Keep
daniel.keep.lists at gmail.com
Mon Oct 19 05:08:55 PDT 2009
Zarathustra wrote:
> Could somebody explain memory rules in class inheriting?
> The following code illustrates my problem, look at 'main' function:
Posts like this belong on d.learn.
> //____________________________________________
> module window;
>
> private import base;
> private import structs;
>
> private static import user32;
> private static import kernel32;
> private static import gdi32;
imports are private by default.
> 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);
> }
You shouldn't be using Window*: see below.
> 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;
What is ptr? If it's void*, you don't need to do this.
Incidentally, there's already a set of bindings for the Win32 api. Not
sure where the current version is; somewhere on dsource.org.
> 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";
Easier to type "clsname"w than cast(wstr)"clsname"
> wndClass.hIconSm = user32.loadIcon(null, 0x00007F00);
>
> ...
>
> handle = user32.createWindowEx(
> 0,
> wndClass.className,
> cast(wstr)"<applicationcaption>",
> 0x00CF0000,
> 0x00000000,
> 0x00000000,
> 0x00000280,
> 0x000001E0,
> null,
> null,
> kernel32.getModuleHandle(null),
> cast(ptr)&this
> );
this is a reference to the object. &this is the address at which the
reference is being stored, which is probably *on the stack*.
I wouldn't be surprised if this is why it's segfaulting.
> if(handle is null){
> user32.messageBox(null, user32.translateErrorCode(kernel32.getLastError()), cast(wstr)"error", 0x00000000);
> assert(false, "window creating failed");
> }
> }
>
> ...
>
> class MyWindow : Window{
> this(){
> super();
> }
>
> override void onMouseDown(MouseEventArgs o_mea){
> MsgBox(cast(char[])"Hello");
Unless MsgBox is defined very strangely, the cast(char[]) here should be
superfluous.
> }
>
> }
>
> ...
More information about the Digitalmars-d
mailing list