Bug 6667 – Object.factory creates instance of abstract classes.

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-09-14T04:55:00Z
Last change time
2011-09-14T05:36:40Z
Assigned to
nobody
Creator
opantm+spam

Comments

Comment #0 by opantm+spam — 2011-09-14T04:55:22Z
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 by yebblies — 2011-09-14T05:36:40Z
*** This issue has been marked as a duplicate of issue 1692 ***