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. #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

  3. #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)
We are about to migrate away from a class C ip structure (192.168.100.x) to class B (http://www.subnet-calculator.com/subnet.php?net_class=B) I...
Server General
Referencing enumeration
The MSDN page http://msdn.microsoft.com/en-us/library/bb774096(VS.85).aspx documents the ShellSpecialFolderConstants enumerated type that assigns...
PowerShell
When a class is both an inherited class of another, and alsoimplements an interface method
The code below doesn't compile (after I added ": IComparable" on the first line). Any advice on how to get it compiled? public class...
.NET General
Referencing Posts ?
Hi, Does anyone know how to put a link into a post that refers to another post and which only refers to that particular post and not any others....
Vista General
Access a static member on a nested static class.
Hi Everyone, I was playing around with Powershell yesterday, trying to write functions to act as a simple FTP client. I based my code around the...
PowerShell
Access a static member on a nested static class.
Hi Everyone, I was playing around with Powershell yesterday, trying to write functions to act as a simple FTP client. I based my code around the...
PowerShell
Calling an XPath on a Binding with an XmlNode causes StackOverflowException
I've been getting a hell of a lot more StackOverflows deep inside the framework (WindowsBase.Utility.LargeSortedObjectMap.GetKeyValuePair) than I'd...
Avalon