[Issue 9500] Interfaces - shared static this

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 17 07:25:44 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9500



--- Comment #5 from Maxim Fomin <maxim at maxim-fomin.ru> 2013-02-17 07:25:43 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list