Jump to page: 1 2
Thread overview
Pointers, casting, SetGetWindowLong problem...
Mar 09, 2007
Chris Warwick
Mar 09, 2007
Derek Parnell
Mar 10, 2007
Chris Warwick
Mar 10, 2007
Frits van Bommel
Mar 10, 2007
Chris Warwick
Mar 10, 2007
Chris Warwick
Mar 10, 2007
Frits van Bommel
Mar 09, 2007
Frits van Bommel
Mar 09, 2007
Bill Baxter
Mar 10, 2007
Bradley Smith
Mar 10, 2007
torhu
Mar 10, 2007
Bradley Smith
Mar 10, 2007
Kristian Kilpi
Mar 10, 2007
Bradley Smith
Mar 11, 2007
Kristian Kilpi
Mar 10, 2007
Chris Warwick
Mar 10, 2007
Bill Baxter
March 09, 2007
Hi, i use this to create and show my window

            HINSTANCE hinstance = GetModuleHandleA(null);
            fhandle = CreateWindowExA(
                WS_EX_APPWINDOW, "CjWindow", "Testing 123", WS_TILEDWINDOW,
                x, y, width, height, HWND_DESKTOP, cast(HMENU) null,
                hinstance, null);
            SetWindowLongA(fhandle, 0, cast(LONG) cast(VOID*) this);
            ShowWindow(fhandle, SW_SHOW);

And this in my WindowProc

    TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle, 0);
    if (window == null)  return DefWindowProcA(handle, msg, wparam, lparam);

but i keep getting an access violation on the if (window == null),

if i change it to

    if (window == null) beep(400,50)

i still get it, the AV that is, but if i just skip the check and call the DefWindoProcA it works fine.

I cant work out why, even if i have fecked up the casting or setting of the the windowLong var, testing what was returned against null shouldnt cause an AV should it?

Anycase, any ideas what I've done wrong?

cheers,

cw


March 09, 2007
On Fri, 9 Mar 2007 23:16:34 -0000, Chris Warwick wrote:


>     TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle, 0);
>     if (window == null)  return DefWindowProcA(handle, msg, wparam, lparam);
> 
> but i keep getting an access violation on the if (window == null),
> 
> if i change it to
> 
>     if (window == null) beep(400,50)
> 
> i still get it, the AV that is, but if i just skip the check and call the DefWindoProcA it works fine.

Use this instead ...

    if (window is null) ...

Because ...

    if (window == null)

is identical to

    if (window.opEquals(null))

which will fail if 'window' is null because it needs to use it to reference the opEquals method.


-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell
March 09, 2007
Chris Warwick wrote:
> 
>     TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle, 0);
>     if (window == null)  return DefWindowProcA(handle, msg, wparam, lparam);
> 
[snip]
> 
> I cant work out why, even if i have fecked up the casting or setting of the
> the windowLong var, testing what was returned against null shouldnt cause an
> AV should it?

Actually, the test is *exactly* what's causing this.
Comparing class references (including null, if the type is a class type) is done by calling a member function on the left operand with the right operand as a parameter (in this case, window.opEquals(null)). To call opEquals, the vtable pointer needs to be found, which produces an access violation for null references (since it needs to dereference the reference to get at the vtable pointer).

What you want to do is not test equality, but testing identity. For that, replace '==' by 'is':
---
    TWindow window = /* whatever */;
    if (window is null)  /* something */;
---
should do what you want.
March 09, 2007
Chris Warwick wrote:
> Hi, i use this to create and show my window
> 
>             HINSTANCE hinstance = GetModuleHandleA(null);
>             fhandle = CreateWindowExA(
>                 WS_EX_APPWINDOW, "CjWindow", "Testing 123", WS_TILEDWINDOW,
>                 x, y, width, height, HWND_DESKTOP, cast(HMENU) null,
>                 hinstance, null);
>             SetWindowLongA(fhandle, 0, cast(LONG) cast(VOID*) this);
>             ShowWindow(fhandle, SW_SHOW);
> 
> And this in my WindowProc
> 
>     TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle, 0);
>     if (window == null)  return DefWindowProcA(handle, msg, wparam, lparam);
> 
> but i keep getting an access violation on the if (window == null),
> 
> if i change it to
> 
>     if (window == null) beep(400,50)
> 
> i still get it, the AV that is, but if i just skip the check and call the
> DefWindoProcA it works fine.
> 
> I cant work out why, even if i have fecked up the casting or setting of the
> the windowLong var, testing what was returned against null shouldnt cause an
> AV should it?
> 
> Anycase, any ideas what I've done wrong?
> 
> cheers,
> 
> cw
> 
> 


This is definitely an F.A.Q.
Is there a D Programming FAQ anywhere?
'Why does "if (someobject == null)" keep crashing?' would definitely be there.
What else?

--bb
March 10, 2007
Bill Baxter wrote:
> This is definitely an F.A.Q.
> Is there a D Programming FAQ anywhere?
> 'Why does "if (someobject == null)" keep crashing?' would definitely be there.
> What else?
> 
> --bb

I've started recording "surprises" like these on a Web page here: http://www.baysmith.com/d/

  Bradley
March 10, 2007
"Derek Parnell" <derek@psych.ward> wrote in message news:1f2c01vt6zeaq$.ac8d0velaphe$.dlg@40tude.net...
> On Fri, 9 Mar 2007 23:16:34 -0000, Chris Warwick wrote:
>
>
>>     TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle,
>> 0);
>>     if (window == null)  return DefWindowProcA(handle, msg, wparam,
>> lparam);
>>
>> but i keep getting an access violation on the if (window == null),
>>
>> if i change it to
>>
>>     if (window == null) beep(400,50)
>>
>> i still get it, the AV that is, but if i just skip the check and call the DefWindoProcA it works fine.
>
> Use this instead ...
>
>    if (window is null) ...
>
> Because ...
>
>    if (window == null)
>
> is identical to
>
>    if (window.opEquals(null))
>
> which will fail if 'window' is null because it needs to use it to
> reference
> the opEquals method.

So opEquals must be inherited from Object cause I havnt implememented it in my class. So what exactly does this inherited opEquals do? How can an inherited opEquals have any sensible result in descendant classes unless overriden by those subclasses?

cheers,

cw



March 10, 2007
"Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:essrhi$vgr$1@digitalmars.com...
> Chris Warwick wrote:
>> Hi, i use this to create and show my window
>>
>>             HINSTANCE hinstance = GetModuleHandleA(null);
>>             fhandle = CreateWindowExA(
>>                 WS_EX_APPWINDOW, "CjWindow", "Testing 123",
>> WS_TILEDWINDOW,
>>                 x, y, width, height, HWND_DESKTOP, cast(HMENU) null,
>>                 hinstance, null);
>>             SetWindowLongA(fhandle, 0, cast(LONG) cast(VOID*) this);
>>             ShowWindow(fhandle, SW_SHOW);
>>
>> And this in my WindowProc
>>
>>     TWindow window = cast(TWindow) cast(void*)  GetWindowLongA(handle,
>> 0);
>>     if (window == null)  return DefWindowProcA(handle, msg, wparam,
>> lparam);
>>
>> but i keep getting an access violation on the if (window == null),
>>
>> if i change it to
>>
>>     if (window == null) beep(400,50)
>>
>> i still get it, the AV that is, but if i just skip the check and call the DefWindoProcA it works fine.
>>
>> I cant work out why, even if i have fecked up the casting or setting of
>> the
>> the windowLong var, testing what was returned against null shouldnt cause
>> an
>> AV should it?
>>
>> Anycase, any ideas what I've done wrong?
>>
>> cheers,
>>
>> cw
>>
>>
>
>
> This is definitely an F.A.Q.
> Is there a D Programming FAQ anywhere?
> 'Why does "if (someobject == null)" keep crashing?' would definitely be
> there.

It would certainly help if this was mentioned in the docs section on classes.

cheers,

cw


March 10, 2007
Chris Warwick wrote:
> "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:essrhi$vgr$1@digitalmars.com...

>> This is definitely an F.A.Q.
>> Is there a D Programming FAQ anywhere?
>> 'Why does "if (someobject == null)" keep crashing?' would definitely be there.
> 
> It would certainly help if this was mentioned in the docs section on classes.

It does now.  :-)  At least on the comments page.

--bb
March 10, 2007
Chris Warwick wrote:
> "Derek Parnell" <derek@psych.ward> wrote in message news:1f2c01vt6zeaq$.ac8d0velaphe$.dlg@40tude.net...
>> On Fri, 9 Mar 2007 23:16:34 -0000, Chris Warwick wrote:
>>
>>>     if (window == null)  return DefWindowProcA(handle, msg, wparam, lparam);
>>>
>>> but i keep getting an access violation on the if (window == null),
>>>
>> Use this instead ...
>>
>>    if (window is null) ...
>>
>> Because ...
>>
>>    if (window == null)
>>
>> is identical to
>>
>>    if (window.opEquals(null))
>>
>> which will fail if 'window' is null because it needs to use it to reference
>> the opEquals method.
> 
> So opEquals must be inherited from Object cause I havnt implememented it in my class. So what exactly does this inherited opEquals do? How can an inherited opEquals have any sensible result in descendant classes unless overriden by those subclasses?

Ironically, it returns the result of "this is other" :P.
Unfortunately, execution never gets that far when the left-hand side of '==' actually *is* null...
But yes, that method is indeed meant to be overridden by most classes that want to be equality-comparable.
March 10, 2007
"Frits van Bommel" <fvbommel@REMwOVExCAPSs.nl> wrote in message news:est2j6$1f2d$1@digitalmars.com...
> Chris Warwick wrote:
>> "Derek Parnell" <derek@psych.ward> wrote in message news:1f2c01vt6zeaq$.ac8d0velaphe$.dlg@40tude.net...
>>> On Fri, 9 Mar 2007 23:16:34 -0000, Chris Warwick wrote:
>>>
>>>>     if (window == null)  return DefWindowProcA(handle, msg, wparam,
>>>> lparam);
>>>>
>>>> but i keep getting an access violation on the if (window == null),
>>>>
>>> Use this instead ...
>>>
>>>    if (window is null) ...
>>>
>>> Because ...
>>>
>>>    if (window == null)
>>>
>>> is identical to
>>>
>>>    if (window.opEquals(null))
>>>
>>> which will fail if 'window' is null because it needs to use it to
>>> reference
>>> the opEquals method.
>>
>> So opEquals must be inherited from Object cause I havnt implememented it in my class. So what exactly does this inherited opEquals do? How can an inherited opEquals have any sensible result in descendant classes unless overriden by those subclasses?
>
> Ironically, it returns the result of "this is other" :P.
> Unfortunately, execution never gets that far when the left-hand side of
> '==' actually *is* null...
> But yes, that method is indeed meant to be overridden by most classes that
> want to be equality-comparable.

Seems odd that it's even implemented at all, = is not overloadable if i understood the docs right, and I cant see add or sub being overloaded in Object.. So why implement opEquals? Especialy when it's basicly worse than (obj1 is obj2).

Unless it's a cunning ploy, using AV faults to cattle prod people into using 'is'.





« First   ‹ Prev
1 2