D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15721 - free error when calling Mallocator.dispose on interfaces
Summary: free error when calling Mallocator.dispose on interfaces
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-25 10:29 UTC by joeyemmons
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description joeyemmons 2016-02-25 10:29:33 UTC
Seems calling dispose on an interface does not work. The following will cause free to fail:

// someOutputRange is a ubyte[] output range of type O
auto malloc = Mallocator.instance;
OutputRange!(ubyte[]) range;
range =  malloc.make!(OutputRangeObject!(O, ubyte[]))(someOutputRange);

// do some stuff...

malloc.dispose(range); 


It fails because typeid on interfaces is wonky, who thought that was a good idea...

It can be fixed by changing to: 
malloc.dispose(cast(Object)range); 
But honestly it should not be necessary and will 100% trip people up.
Comment 1 basile-z 2016-02-26 05:43:04 UTC
GCAllocator is supported but Mallocator produces a free error:


import std.experimental.allocator.mallocator;
import std.experimental.allocator.gc_allocator;
import std.experimental.allocator;

interface Foo {}
class Bar: Foo {}

void main()
{
    GCAllocator.instance.dispose(cast(Foo) GCAllocator.instance.make!Bar);
    // comment the following line: OK
    Mallocator.instance.dispose(cast(Foo) Mallocator.instance.make!Bar);
}
Comment 2 basile-z 2016-02-26 05:52:59 UTC
I think this is safe to change dispose to:

void dispose(A, T)(auto ref A alloc, T p)
if (is(T == class) || is(T == interface))
{
    if (!p) return;
    auto support = (cast(void*) cast(Object) p)[0 .. typeid(p).init.length];
    destroy(p);
    alloc.deallocate(support);
}

deallocate doesn't care about the the length of initial static layout of a class.
Comment 3 github-bugzilla 2016-03-03 13:52:06 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/211c10aff2b2cdfd9280f159cab7825b8a323c80
fixed issue 15721

https://github.com/D-Programming-Language/phobos/commit/e55e196127c75899a36016cba12b8cbd5c1548d9
Merge pull request #4022 from BBasile/issue-15721

fixed issue 15721 - std.experimental.allocator dispose on interface