[Issue 1282] Very strange GC problem, memory corruption
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 21 01:30:00 PDT 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1282
deewiant at gmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |INVALID
------- Comment #4 from deewiant at gmail.com 2007-06-21 03:29 -------
I found the problem, and it's my code: I'm deleting a pointer to a struct which
hasn't been allocated with new. It's a remnant from when I used a class instead
of a struct. It boils down to the following:
struct S {}
S* ps;
void main() {
S s;
ps = &s;
delete ps;
// assign ps to something else and keep doing stuff...
}
http://www.digitalmars.com/d/expression.html#DeleteExpression says: "If the
garbage collector was not used to allocate the memory for the instance,
undefined behavior will result."
I can't reproduce the problem after removing the delete, so I'll assume that
was it: the GC allocates memory for a char[] array on top of
deleted-but-not-newed memory, which happens to be on top of a class reference
in a struct. This would explain why the class reference becomes 0xffffffff
instead of, say, null: char.init is 0xff.
Those Access Violations would just be harder-to-find symptoms of the same.
Of course, if I'm unlucky, something else causes the problem and the delete is
just one of those unrelated, yet relevant, lines, but I don't think so.
My bad! Nothing to see here!
Unless there's a way for the GC to stop this from ever happening accidentally?
Couldn't it know which areas in memory it has allocated?
--
More information about the Digitalmars-d-bugs
mailing list