www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1227] New: Access Violation when using comparison of template class pointer

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1227

           Summary: Access Violation when using comparison of template class
                    pointer
           Product: D
           Version: 1.014
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: aurelien.vermifuge gmail.com


THe compiler seem to have a problem with template class. I think the code will
be more usefull than a long description with ma chaotic english.

I made that :
---- util/linkedlist.d ----
class LinkedList(TypeList=Object)
{
        private LinkedListLink!(TypeList) first, last;
        private int count;

        public this()
        {
                this.first = null;
                this.last = null;

                this.count = 0;
        }

        public void add(TypeList T)
        {
                LinkedListLink!(TypeList) n;

                synchronized(this)
                {
                        try
                        {
                                n = new LinkedListLink!(TypeList) (last, T);
                        } catch (OutOfMemoryException e)
                        {
                                throw e;
                        }

                        if ( this.last==null )
                        {
                                this.first = n;
                                this.last = n;
                        } else
                        {
                                this.last.next = n;
                                this.last = n;
                        }

                        count++;
                }
        }
}

class LinkedListLink(TypeList=Object)
{
        public TypeList value;
        public LinkedListLink next, previous;

        public this(TypeList T)
        {
                value = T;
                next = null;
                previous = null;
        }

        public this(LinkedListLink prev, TypeList T)
        {
                value = T;
                next = null;
                previous = prev;
        }
}




---- main.d ----
int main(char[][] args)
{
        LinkedList!(int) listEntiers;

        try
        {
                listEntiers = new LinkedList!(int)();
        } catch (OutOfMemoryException e)
        {
                printf("No more memory u_u");
        }

        for (int i=0; i<15; i++)
        {
                try
                {
                        listEntiers.add(i);
                } catch (OutOfMemoryException e)
                {
                        printf("BUG");
                }
        }
}



-------------------------------
It compile well, but when the add is called, the programm make an "Access
Violation" and stop running. I have looked at the ASM generated code, and I saw
that :

( eax = pointer to object )
mov eax, [eax+0Ch] // Put the this.last value into eax, the value is null
mov eax, [eax]     // Try to read into the object à the offset 0 --> Exception


If I put : this.last = null; it works well, dans the generated code is :
( eax = pointer to object )
mov [eax+0Ch], 0   // Correct

but the condition is not well formed.

Hope it will be usefull.


-- 
May 11 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:bug-1227-3 http.d.puremagic.com/issues/...
 http://d.puremagic.com/issues/show_bug.cgi?id=1227

Not a bug. Use 'is' to see if a reference is null, not '=='.
        public void add(TypeList T)
        {
                LinkedListLink!(TypeList) n;

                synchronized(this)
                {
                        try
                        {
                                n = new LinkedListLink!(TypeList) (last, 
 T);
                        } catch (OutOfMemoryException e)
                        {
                                throw e;
                        }

                        if ( this.last==null )

This last line should be: if( this.last is null )
                        {
                                this.first = n;
                                this.last = n;
                        } else
                        {
                                this.last.next = n;
                                this.last = n;
                        }

                        count++;
                }
        }
 } 

May 11 2007
parent david <ta-nospam-zz gmx.at> writes:
Jarrett Billingsley schrieb:
 <d-bugmail puremagic.com> wrote in message 
 news:bug-1227-3 http.d.puremagic.com/issues/...
 http://d.puremagic.com/issues/show_bug.cgi?id=1227

Not a bug. Use 'is' to see if a reference is null, not '=='.
        public void add(TypeList T)
        {
                LinkedListLink!(TypeList) n;

                synchronized(this)
                {
                        try
                        {
                                n = new LinkedListLink!(TypeList) (last, 
 T);
                        } catch (OutOfMemoryException e)
                        {
                                throw e;
                        }

                        if ( this.last==null )

This last line should be: if( this.last is null )
                        {
                                this.first = n;
                                this.last = n;
                        } else
                        {
                                this.last.next = n;
                                this.last = n;
                        }

                        count++;
                }
        }
 } 


Reason: if (window == null) is another way of saying: if (window.opEquals(null)) but if "window" is null, it will fail because it needs "window" for the reference of the opEquals method to work. david
May 11 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1227


deewiant gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from deewiant gmail.com  2007-05-11 13:07 -------
Invalid, as explained in the NG: use "is", not "==" to compare object pointers,
as the latter transforms into a call to object.opEquals, which will fail if the
object is null.


-- 
May 11 2007