www.digitalmars.com         C & C++   DMDScript  

D - [bug?] access violation on custom new / delete

reply h3r3tic <h3r3tic_member pathlink.com> writes:
hiya. this code causes an access violation:
------------------------------------------------------------

import std.c.stdlib;
import std.outofmemory;
import std.gc;

class Foo
{
new(uint sz)
{
void* p;

p = std.c.stdlib.malloc(sz);
if (!p)
throw new OutOfMemory();
std.gc.addRange(p, p + sz);
return p;
}

delete(void* p)
{
if (p)
{	std.gc.removeRange(p);
std.c.stdlib.free(p);
}
}
}



void main() 
{
printf("new...\n");
Foo bar = new Foo;
printf("delete...\n");
delete bar;
printf("fatality...\n");
}

------------------------------------------------------------

am i missing sth or is that a bug ? obviously the code comes from
"http://www.digitalmars.com/d/memory.html", though i had to add "std." before
'gc.*' to make it compile <- the documentation needs a change.

compile log = """
------------------------------------------------------------
D:\dmd\bin\..\..\dm\bin\link.exe
C:\h3\d\dide\Projects\Temp\Temp,Temp.exe,,user32+kernel32/noi;
D:\dmd\bin\dmd.exe   -odC:\h3\d\dide\Projects\Temp -ofTemp.exe
"C:\h3\d\dide\Projects\Temp\Temp.d"
------------------------------------------------------------
Build started on 4/4/2004 at 13:13:58



Build Succeeded
"""

OUTPUT = """
C:\H3\D\dide\Projects\Temp>temp
new...
delete...
Error: Access Violation
"""

im running WindowsXP SP1 on AthlonXP. Im using the most recent version of dmd.
Apr 04 2004
parent "Manfred Nowak" <svv1999 hotmail.com> writes:
h3r3tic wrote:

[...]
 am i missing sth or is that a bug ?

Both. Foo is missing any "normal" declaration. So it is completely useless. Declaring something, I used `int x;', makes the error vanish. Anyway this shouldn't cause an AV or an "Win32 exception", which throws when deleting the printf. So long!
Apr 04 2004