Bug 907 – pointers in static constructors are wack
Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-01-30T09:04:00Z
Last change time
2014-02-16T15:23:43Z
Assigned to
bugzilla
Creator
funisher
Comments
Comment #0 by funisher — 2007-01-30T09:04:21Z
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
}
Comment #1 by shro8822 — 2007-01-30T11:15:28Z
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.