The documentation / spec [1][2] states that realloc and free should do nothing when they're passed interior pointers. Currently realloc / free still operate on the underlying memory blocks regardless. I started working on the fix for realloc but decided that it should really be discussed before spending more time on it. Discuss :) ---- [1] http://dlang.org/phobos/core_memory.html#.GC.realloc [2] http://dlang.org/phobos/core_memory.html#.GC.free
I suppose this should have been spit into two bugs, but for now we can keep the discussion focused. The reason fixing realloc requires discussion is that fixing this may break exiting code in the case where it is passed an interior pointer with a non zero size parameter. Fixing the case where the pointer is internal and either realloc is called with size zero or free is called does not break existing code unless the GC is disabled, where it will cause memory leaks.
Discussion thread: http://forum.dlang.org/thread/qmiayckycycetnumulbn@forum.dlang.org
https://github.com/dlang/druntime/pull/2436
Commits pushed to master at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/e6087c2fed9e49daaa72296bd951ea56efe82ec7 fix Issue 11393,11446 - [GC] GC realloc and free don't ignore interior/non-GC owned pointers change specification of GC.realloc to something more robust: - return null for failure (returning the original pointer is no indication of failure as it might also be returned on success. This can easily lead to overwriting memory) - allow freeing old memory immediately (previous implementation did this for large objects when shrinking) - only set attribute bits on existing block if reused (previous implementation didn't set bits if it reused a small block) refactor realloc to match that spec, add changelog, fix example https://github.com/dlang/druntime/commit/c1c0a85f5904953b98b2e1b188a650576b83da82 Merge pull request #2436 from rainers/gc_realloc fix Issue 11393 - [GC] GC realloc and free don't ignore interior pointers merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
dlang/druntime pull request #3149 "fix test for issue 11393 introduced by #2436" was merged into master: - d247d9636ca568b37d7995a253e525b41b6e2d38 by Rainer Schuetze: fix test for issue 11393 https://github.com/dlang/druntime/pull/3149