www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2293] New: access instance members from inside delegates

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

           Summary: access instance members from inside delegates
           Product: D
           Version: 1.034
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: enzo.petrelli fastwebnet.it


/***************************
        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;
                }
        }
}


-- 
Aug 19 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2293


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com





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.


-- 
Nov 24 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2293


maxmo pochta.ru changed:

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





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.


-- 
Nov 25 2008