On 2012-12-27 16:31, bearophile wrote:
Don't cast arrays arrays to pointers, it's unclean, because dynamic arrays are
two words long. Use the .ptr property:
GC.free(s.ptr);
Things appeared sorted out, but it's not over yet. delete still wins.
Performance-wise free() worked like delete, but there's a nasty glitch.
The following code crashes after a few hundred loops with
core.exception.InvalidMemoryOperationError.
import std.stdio, core.memory;
char[1000] filler = '.';
int fill_times = 10000;
void main(string[] args) {
char[] s;
for (int i=0; i < 100000; i++) {
for (int j=0; j < fill_times; j++)
s ~= filler;
writeln("loop ", i + 1, ", length: ", s.length);
stdout.flush();
// delete s;
GC.free(s.ptr);
s = [];
}
}
Surprisingly, when fill_times >= 3736, it always crashes after the 341th loop
(relies on the filler having 1000 bytes. Some other numbers also trigger it).
What could be responsible for this odd behavior of free()?