www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Object disappearing

reply "Ben Harper" <ben gls.co.za> writes:
I have a class object, called MsBaseWindow.

-- I do:

{
MsBaseWindow wnd = new MsBaseWindow;
Table[0].Window = wnd;
assert( Table[0].Window.hWnd != null );
Run();
}


-- Inside Run():

if ( Table[0].Window != null )
{
    HWND ww = Table[0].Window.hWnd;  // Access violation reading address 
0x00000008 !

}

--  Table is an array whos members are structures which for clarity look 
like this:
struct TableItem
{
    MsBaseWindow* Window;
}

so it's
TableItem[] Table;

I looked at the assembly generated by the lookup of hWnd in both instances
(the assertion and the latter lookup that causes the violation), and what I 
notice
is that ESI loads address of the Table[0].Window object, which remains 
consistent.
And then the first few bytes of the memory pointed to be ESI seem to be the 
object
info. Now when the object is created and first stored into the Table, it's 
data is fine, and
the assertions passes. But later on, during Run() (which shortly afterwards, 
but probably
long enough afterwards for the GC to have done something), I notice that the 
data
at the beginning of the ESI memory is all zero. Does this mean that the 
object has been
cleaned up or moved by the GC or something like that? As far as I can tell 
everything I'm
doing is legal.

Any help would be appreciated..
Thanks.
May 13 2005
parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
Try without the "*" in the struct:

struct TableItem
{
    MsBaseWindow Window;
}

(This is probably what you meant anyway.)

L. 
May 13 2005
parent reply "Ben Harper" <ben gls.co.za> writes:
Is there a good explanation somewhere of the reference/value semantics used? 
I can't figure it out or find it.

"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message 
news:d6280d$269p$1 digitaldaemon.com...
 Try without the "*" in the struct:

 struct TableItem
 {
    MsBaseWindow Window;
 }

 (This is probably what you meant anyway.)

 L.
 
May 13 2005
parent David Medlock <noone nowhere.com> writes:
Ben Harper wrote:
 Is there a good explanation somewhere of the reference/value semantics used? 
 I can't figure it out or find it.
 
 "Lionello Lunesu" <lio lunesu.removethis.com> wrote in message 
 news:d6280d$269p$1 digitaldaemon.com...
 
Try without the "*" in the struct:

struct TableItem
{
   MsBaseWindow Window;
}

(This is probably what you meant anyway.)

L.
Its actually extremely simple. Structs and base types(int,float,char,pointers,etc) are always passed by value. Classes and arrays are passed by reference(implicit pointer). -DavidM
May 13 2005