digitalmars.D.bugs - Custom allocator + empty class = crash
- Nick <Nick_member pathlink.com> Feb 15 2006
- Thomas Kuehne <thomas-dloop kuehne.cn> Feb 16 2006
The following is a modified version of the custom allocator example in the docs.
The class has to be empty, ie. without any members, for the crash to occur.
Still I thought I should report it since it might be a symptom of a deeper
problem.
# import std.c.stdlib;
# import std.outofmemory;
# import std.gc;
# import std.stdio;
#
# class Foo
# {
# new(uint sz)
# {
# void* p;
#
# p = std.c.stdlib.malloc(sz);
#
# if (!p) throw new OutOfMemoryException();
#
# addRange(p, p + sz);
#
# writefln("Allocated %d bytes at ", sz, p);
#
# return p;
# }
#
# delete(void* p)
# {
# writefln("Freeing ", p); // This line is never reached
# if (p)
# {
# removeRange(p);
# std.c.stdlib.free(p);
# }
# writefln(p, " is free!");
# }
# }
#
# void main()
# {
# Foo f = new Foo;
# delete f; // The delete command causes the crash
# }
Output:
Allocated 8 bytes at 93072f8
Segmentation fault
Nick
Feb 15 2006
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Nick schrieb am 2006-02-15:The following is a modified version of the custom allocator example in the docs. The class has to be empty, ie. without any members, for the crash to occur. Still I thought I should report it since it might be a symptom of a deeper problem. # import std.c.stdlib; # import std.outofmemory; # import std.gc; # import std.stdio; # # class Foo # { # new(uint sz) # { # void* p; # # p = std.c.stdlib.malloc(sz); # # if (!p) throw new OutOfMemoryException(); # # addRange(p, p + sz); # # writefln("Allocated %d bytes at ", sz, p); # # return p; # } # # delete(void* p) # { # writefln("Freeing ", p); // This line is never reached # if (p) # { # removeRange(p); # std.c.stdlib.free(p); # } # writefln(p, " is free!"); # } # } # # void main() # { # Foo f = new Foo; # delete f; // The delete command causes the crash # } Output: Allocated 8 bytes at 93072f8 Segmentation fault Nick
Added to DStress as http://dstress.kuehne.cn/run/d/delete_12_A.d http://dstress.kuehne.cn/run/d/delete_12_B.d http://dstress.kuehne.cn/run/d/delete_12_C.d http://dstress.kuehne.cn/run/d/delete_12_D.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFD9EMK3w+/yD4P9tIRAmyXAJ9/uqdrM4x55Kf8fkvlZG4NK/gsLACeJGly AdqRgzjnee64qWTt6OiBaZI= =pjcJ -----END PGP SIGNATURE-----
Feb 16 2006








Thomas Kuehne <thomas-dloop kuehne.cn>