D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6667 - Object.factory creates instance of abstract classes.
Summary: Object.factory creates instance of abstract classes.
Status: RESOLVED DUPLICATE of issue 1692
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-14 04:55 UTC by Kapps
Modified: 2011-09-14 05:36 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kapps 2011-09-14 04:55:22 UTC
Object.factroy successfully creates an invalid instance when used for an abstract class. There is no way to distinguish that the instance is invalid, as it is not null. It may be assigned, but upon first access it will result in a segfault. This also means that (AFAIK), short of looking at the vtbl, you have no way of knowing if a class is abstract using Typeinfo. Object.factory is supposed to return null when errors occur. Tested on Windows 7 64-bit (compiled as 32-bit) and Linux 64-bit.

Example:

import std.stdio;
import std.traits;
import std.conv;

abstract class C {
        string AbstractMethod();
}

C[] Objects;

void main() {
        TypeInfo_Class ti = typeid(C);
        C MyC = cast(C)Object.factory(ti.name);
        writefln("Created " ~ ti.name ~ " - IsNull: " ~ to!string(MyC is null) ~ " -  Location: " ~ to!string(&MyC) ~ ".");
        Objects ~= MyC;
        writefln("Added to collection.");
        writefln(Objects[0].AbstractMethod());
}


Output:

Created temptest.C - IsNull: false -  Location: 7FFF15772900.
Added to collection.
Segmentation fault
Comment 1 yebblies 2011-09-14 05:36:40 UTC

*** This issue has been marked as a duplicate of issue 1692 ***