[Issue 364] New: Mysterious access violation when using delegate()[1]... parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Sep 23 14:44:02 PDT 2006


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

           Summary: Mysterious access violation when using delegate()[1]...
                    parameter
           Product: D
           Version: 0.167
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: smjg at iname.com


One case of delegate parameters used for lazy evaluation of expressions has
been kept, namely typesafe variadic parameters.  However, the following use
causes an access violation to be thrown for no apparent reason.

----------
import std.stdio;

abstract class Comb {
    Comb opCall(Comb delegate()[1] c...) {
        static int level = -1;
        level++;

        writefln("%*sCO: %s", level, "", this);
        Comb ec = c[0]();
        writefln("%*sCO: %s to %s", level, "", this, ec);
        Comb result = apply(ec);
        writefln("%*sCO: %s to %s, result: %s", level, "", this, ec, result);

        level--;
        return result;
    }

    abstract Comb apply(Comb c);
}


class SComb2 : Comb {
    Comb c1;
    Comb c2;

    this(Comb a1, Comb a2) {
        c1 = a1;
        c2 = a2;
    }

    Comb apply(Comb c)
    out (result) {
        writefln("SA: %s to %s, result: %s", this, c, result);
    }
    body {
        writefln("SA: %s to %s", this, c);
        return c1.apply(c)(c2(c));
    }

    char[] toString() {
        return "S[" ~ c1.toString ~ ", " ~ c2.toString ~ "]";
    }
}


class IComb : Comb {
    Comb apply(Comb c) {
        return c;
    }

    char[] toString() {
        return "I";
    }
}


void main() {
    Comb I = new IComb;

    /*  This represents the combinator expression
     *  S I I I
     */
    (new SComb2(I, I)) (I);
}
----------
CO: S[I, I]
CO: S[I, I] to I
SA: S[I, I] to I
 CO: I
  CO: I
  CO: I to I
  CO: I to I, result: I
 CO: I to I
 CO: I to I, result: I
SA: S[I, I] to I, result: I
Error: Access Violation
----------

As you see, it happens between SComb2.apply returning and the next statement in
Comb.opCall being executed.


If the statement in main is changed to

    (new SComb2(I, I)).apply(I);

then the same happens, except that (as expected) the first two lines above
aren't printed.  If I also add

    writefln("CO");

after the apply call in Comb.opCall, then the error message changes to the even
more cryptic

    Error: Win32 Exception


-- 




More information about the Digitalmars-d-bugs mailing list