www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9500] New: Interfaces - shared static this

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500

           Summary: Interfaces - shared static this
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: admin dav1d.de



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)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 12 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



15:14:22 PST ---
 * or shared static this is changed to:
 
 shared static this() {
     IFace i = new Bla();
     m = new Multi([i]);
 }
Simpler: m = new Multi([new Bla()]); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Interfaces - shared static  |Regression (2.061):
                   |this                        |Interfaces - shared static
                   |                            |this
           Severity|normal                      |regression



15:15:41 PST ---
Introduced in 2.061. 2.060 works.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Regression (2.061):         |Interfaces - shared static
                   |Interfaces - shared static  |this
                   |this                        |
           Severity|regression                  |normal



15:35:27 PST ---

 Introduced in 2.061. 2.060 works.
Not a regression, I've read the wrong status. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru



---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500




---
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: -------
Feb 17 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9500


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



---
See issue 9527

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