www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 907] New: pointers in static constructors are wack

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

           Summary: pointers in static constructors are wack
           Product: D
           Version: 1.004
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: funisher gmail.com


I get strange things when I try to initialize variables in the static
constructors. It's quite easy to just move the variable outside into the class,
so it's not a severe bug.. also, once the pointer is initialized, it works ok
from then on... here's an example:

import std.stdio;
static class Lala {
        static:
        void delegate() lala2;
        void delegate() lala3;
        void delegate() lala4;
        void delegate() ok2;

        static this() {
                uint* lala;
                uint* ok = null;
                lala2 = delegate() { writefln("inside delegate: should be
'0000': ", lala); lala = new uint; *lala = 5; };
                lala3 = delegate() { writefln("inside delegate: should be
'0000': ", lala, " and the value is: ", *lala); lala = null; };
                lala4 = delegate() { writefln("inside delegate: should be
'0000': ", lala); };
                ok2 = delegate() { writefln("inside delegate: should be '0000':
", ok); };
                writefln("outside delegate: should be '0000': ", lala, " and
'0000': ", ok);
        }
}

void main() {
        Lala.lala2(); // bad
        Lala.lala3(); // behaves as expected
        Lala.lala4(); // behaves as expected
        Lala.ok2(); // bad
}


-- 
Jan 30 2007
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=907


shro8822 uidaho.edu changed:

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





This is invalid. all of the delegate inside of the "static this" use stack
variables, once the "static this" returns, the stack variables are unusable
(but the delegate doesn't known this). 

Anon delegates should NEVER be allowed to escape the function in which they are
defined.

The bug here is that the docs don't have this in big red font.


-- 
Jan 30 2007