Windows Vista Forums

Simple class with self-referencing member fail to run: StackOverflowException

  1. #11


    Jack Jackson Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    When Instance #1 is instantiatied, the first thing that happens is the
    fields are initialized. The first field (GoodStone) creates another
    instance of Tire, call that Instance #2.

    Instance #2 starts to intialize its fields. The first field,
    GoodStone, creates another instance of Tire, call that Instance #3.

    Instance #3 starts to initialize its fields. The first field,
    Goodstone, creates another instance of Tire, call that Instance #4.

    And so on, until all memory is exhausted. All of this occurs in one
    thread of execution, so no instance of Tire ever gets past the
    initialization of the GoodStone field.

    If you set a breakpoint on the initialization of the second field you
    should see that it never gets executed.

    On Sat, 29 Nov 2008 05:43:21 GMT, "pedestrian via DotNetMonster.com"
    <u16758@xxxxxx> wrote:

    >Why is the instantiation of a Tire and subsequent Tire instantiation
    >didn't call the constructor? I never see the Console.WriteLine text being
    >showed...
    >
    >I appreciate your explanation.
    >
    >Jack Jackson wrote:

    >>The first instantiation of a Tire attempts to create a new Tire for
    >>GoodStone. The creation of that Tire attempts to create a new Tire
    >>for its Goodstone. The creation of that Tire attempts to create a new
    >>Tire for its Goodstone.
    >>
    >>You can see where this is going. It never gets past initializing
    >>Goodstone in any instance.
    >>

    >>>Yes, fields are initialized first...
    >>>
    >>[quoted text clipped - 11 lines]

    >>>>Fields are initialized before the constructor code executes. That causes
    >>>>the recursion with no output.

      My System SpecsSystem Spec

  2.   


  3. #12


    Family Tree Mike Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    "Jack Jackson" <jjackson@xxxxxx> wrote in message
    news:sf13j4homrv0sekjkcidvo6trksp29apcn@xxxxxx

    > When Instance #1 is instantiatied, the first thing that happens is the
    > fields are initialized. The first field (GoodStone) creates another
    > instance of Tire, call that Instance #2.
    >
    > Instance #2 starts to intialize its fields. The first field,
    > GoodStone, creates another instance of Tire, call that Instance #3.
    >
    > Instance #3 starts to initialize its fields. The first field,
    > Goodstone, creates another instance of Tire, call that Instance #4.
    >
    > And so on, until all memory is exhausted. All of this occurs in one
    > thread of execution, so no instance of Tire ever gets past the
    > initialization of the GoodStone field.
    >
    > If you set a breakpoint on the initialization of the second field you
    > should see that it never gets executed.
    >
    > On Sat, 29 Nov 2008 05:43:21 GMT, "pedestrian via DotNetMonster.com"
    > <u16758@xxxxxx> wrote:
    >

    >>Why is the instantiation of a Tire and subsequent Tire instantiation
    >>didn't call the constructor? I never see the Console.WriteLine text being
    >>showed...
    >>
    >>I appreciate your explanation.
    >>
    >>Jack Jackson wrote:

    >>>The first instantiation of a Tire attempts to create a new Tire for
    >>>GoodStone. The creation of that Tire attempts to create a new Tire
    >>>for its Goodstone. The creation of that Tire attempts to create a new
    >>>Tire for its Goodstone.
    >>>
    >>>You can see where this is going. It never gets past initializing
    >>>Goodstone in any instance.
    >>>
    >>>>Yes, fields are initialized first...
    >>>>
    >>>[quoted text clipped - 11 lines]
    >>>>>Fields are initialized before the constructor code executes. That
    >>>>>causes
    >>>>>the recursion with no output.

    Oh, you are right, the second field never gets started. Thanks!

    --
    Mike


      My System SpecsSystem Spec

  4. #13


    pedestrian via DotNetMonster.com Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    Thanks for the clear explanation... Jack.

    Thanks to Mike too....

    Jack Jackson wrote:

    >When Instance #1 is instantiatied, the first thing that happens is the
    >fields are initialized. The first field (GoodStone) creates another
    >instance of Tire, call that Instance #2.
    >
    >Instance #2 starts to intialize its fields. The first field,
    >GoodStone, creates another instance of Tire, call that Instance #3.
    >
    >Instance #3 starts to initialize its fields. The first field,
    >Goodstone, creates another instance of Tire, call that Instance #4.
    >
    >And so on, until all memory is exhausted. All of this occurs in one
    >thread of execution, so no instance of Tire ever gets past the
    >initialization of the GoodStone field.
    >
    >If you set a breakpoint on the initialization of the second field you
    >should see that it never gets executed.
    >

    >>Why is the instantiation of a Tire and subsequent Tire instantiation
    >>didn't call the constructor? I never see the Console.WriteLine text being
    >[quoted text clipped - 15 lines]

    >>>>>Fields are initialized before the constructor code executes. That causes
    >>>>>the recursion with no output.
    --
    Warmest Regards,
    Pedestrian

    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/For...neral/200812/1


      My System SpecsSystem Spec

Page 2 of 2 FirstFirst 12

Simple class with self-referencing member fail to run: StackOverflowException
Similar Threads
Thread Forum
Moving from class C to class B ip address scheme, any tips(DC/DNS) Server General
When a class is both an inherited class of another, and alsoimplements an interface method .NET General
Access a static member on a nested static class. PowerShell
Access a static member on a nested static class. PowerShell
Calling an XPath on a Binding with an XmlNode causes StackOverflowException Avalon