Bug 2293 – access instance members from inside delegates
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2008-08-19T10:16:00Z
Last change time
2014-03-01T00:36:06Z
Assigned to
bugzilla
Creator
enzo.petrelli
Comments
Comment #0 by enzo.petrelli — 2008-08-19T10:16:41Z
/***************************
OS: Windows XP Pro SP2 / Vista SP1
Compiler/linker: Digital Mars D Compiler v1.034
Tango/tangobos Lib: tango-0.99.7-bin-win32-dmd.1.033
Compiled with: no compile/link flag
in the first call of the method test of the DelegateTest class instance,
the delegate defined inside the inner class doesn't access correctly the
outer class' member miVal
in the second call of te same method, from inside a try/catch block the
outer class' delegate also fails to correctly access the miVal member
***************************/
import tango.core.Exception;
import tango.io.Stdout;
void main()
{
DelegateTest test = new DelegateTest;
Stdout.format("outside try/catch").newline;
test.test();
Stdout.newline.format("inside try/catch").newline;
try
{
test.test();
} catch (Exception)
{
}
}
class DelegateTest
{
int miVal;
void delegate() mpDelegate;
InnerClass moInn;
this()
{
miVal = 5;
moInn = new InnerClass;
mpDelegate = {
Stdout.format("OuterTest.delegate this:{} miVal:{}", cast(void*) &this, miVal).newline;
};
Stdout.format("OuterTest.ctor this:{} miVal:{}", cast(void*) &this, miVal).newline;
}
void test()
{
moInn.mpInnerDelegate();
mpDelegate();
}
class InnerClass
{
void delegate() mpInnerDelegate;
this()
{
mpInnerDelegate = {
Stdout.format("InnerClass.delegate this:{} miVal:{}", cast(void*) &this, miVal).newline;
};
Stdout.format("InnerClass.ctor this:{} miVal:{}", cast(void*) &this, miVal).newline;
}
}
}
Comment #1 by smjg — 2008-11-24T09:32:25Z
Is this a compiletime error, a runtime error or some other form of runtime misbehaviour? Please be specific about what happens - post the output, along with what output you believe the program should generate. And assign an appropriate keyword to this issue while you're at it.
Comment #2 by dfj1esp02 — 2008-11-25T04:24:44Z
Works by design. In D1 nested delegates aren't closures and using them after enclosing function exits results in undefined behavior. this is accessed through frame pointer.