Bug 1227 – Access Violation when using comparison of template class pointer
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-05-11T09:45:00Z
Last change time
2014-02-16T15:23:36Z
Assigned to
bugzilla
Creator
aurelien.vermifuge
Comments
Comment #0 by aurelien.vermifuge — 2007-05-11T09:45:04Z
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.
Comment #1 by matti.niemenmaa+dbugzilla — 2007-05-11T13:07:51Z
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.