Bug 9500 – Interfaces - shared static this

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-12T14:54:00Z
Last change time
2013-02-17T07:46:39Z
Assigned to
nobody
Creator
admin

Comments

Comment #0 by admin — 2013-02-12T14:54:00Z
This segfaults: --------- interface IFace { void log(); } class Multi : IFace { IFace[] faces; this(IFace[] faces...) { this.faces = faces; } override void log() { foreach(face; faces) { if(face !is null) { face.log(); } } } } class Bla : IFace { override void log() {} } /+__gshared+/ Multi m; shared static this() { m = new Multi(new Bla()); } void main() { m.log(); } --------- Fixed if: * shared static this -> static this * or shared static this is changed to: shared static this() { IFace i = new Bla(); m = new Multi([i]); } (passing an array explicitly)
Comment #1 by andrej.mitrovich — 2013-02-12T15:14:22Z
> * or shared static this is changed to: > > shared static this() { > IFace i = new Bla(); > m = new Multi([i]); > } Simpler: m = new Multi([new Bla()]);
Comment #2 by andrej.mitrovich — 2013-02-12T15:15:41Z
Introduced in 2.061. 2.060 works.
Comment #3 by andrej.mitrovich — 2013-02-12T15:35:27Z
Not a regression, I've read the wrong status.(In reply to comment #2) > Introduced in 2.061. 2.060 works. Not a regression, I've read the wrong status.
Comment #4 by maxim — 2013-02-17T04:33:27Z
Dmd 2.062 beta, linux, git head. No segfault for original code, but valgrind still complains. Removing shared from module constructor makes error explicit and passing array explicitly fixes program. I guess something is wrong with vararg function here.
Comment #5 by maxim — 2013-02-17T07:25:43Z
import core.stdc.stdio : printf; interface IFace { void log(); } class Multi : IFace { IFace[] faces; this(IFace[] faces...) { this.faces = faces; print(this); } override void log() { print(m); foreach(face; faces) { if(face !is null) { face.log(); } } } } class Bla : IFace { override void log() {} } Multi m; void print(Multi m) { printf("m=%p\n", cast(void*)m); printf("\tm.faces=%p\n", cast(void*)m.faces); printf("\t\tm.faces[0]=%p\n", cast(void*) m.faces[0]); } static this() { m = new Multi(new Bla()); print(m); } void main() { print(m); m.log(); } Example of output: m=0x7fe9f4b2ffc0 m.faces=0x7fff9ca7c070 m.faces[0]=0x7fe9f4b30ff0 m=0x7fe9f4b2ffc0 m.faces=0x7fff9ca7c070 m.faces[0]=0x7fe9f4b30ff0 m=0x7fe9f4b2ffc0 m.faces=0x7fff9ca7c070 m.faces[0]=0x7fff9ca7c290 m=0x7fe9f4b2ffc0 m.faces=0x7fff9ca7c070 m.faces[0]=0x7fff9ca7c290 Depending on compiler switches and whether shared is appended to module ctor, output of third m.faces[0] (in main) can vary. The fourth m.faces[0] may decay to function pointer, first two would be correct and same. Addresses of higher positions are equal in any case. I guess the problem is that array of Ifaces is not allocated when constructed, hence its content varies thought runtime. I also guess that variardic function should not save its array of arguments in general case. This explains way passing actual array fixed program (because it was properly allocated) and why playing around with module ctor didn't help.
Comment #6 by maxim — 2013-02-17T07:46:39Z
See issue 9527