0

I keep getting StackOverFlowException whenever I instantiate an instance of A class. I know it has got something to do with the fact that A.b field is of type derived from class A. But why would that cause an exception?

static void Main(string[] args)
{
     A a = new A(); // exception
}

class A 
{
    private B b = new B();
}

class B:A {}

thank you

1
  • because its a highly contrived question ... i thought it was a troll tbh - but i liked the idea of a questions about self referencing that references the website it is on. I cannot think of a real life example where anyone would or be inclined to do this - butt hen that is probalby just a lack of imiagination on my part, Jun 14, 2010 at 12:27

7 Answers 7

5

Because B is inherited from A, each time you're creating an A, a B is created, which, in turn creates another B. You've essentially created an endless loop of class creation.

4

Here is what is happening under the hood

  1. Create a new instance of A
  2. As a consequence it creates an instance of B for the private field b
  3. B derives from A and hence has it's own field b. At this point it goes back to step #2 and repeats until a stack overflow occurs
1
  • Since same exception is also thrown if I try to initialize A.b field inside A's constructor, I assume that only way around this is to not initialize A.b field at all? Jun 13, 2010 at 19:09
1

Because A has a reference to a class B, which derives from A. Due to constructor chaining, the ctor of B calls the ctor of A, leading to a creation of a new instance of B, which will continue until eternity or a StackOverflowException.

1

Its because constructors are called recursively

1

Because you ave a circular reference

in order to make a B you have to make an A In order to Make an A you have to Make an B

As such it tunnels downwards.

To fix it, dont initialise the private field.

1

If you want your subclass to know about any derivative types, use generics. This is featured heavily in the CSLA.NET Framework.

public class A<T> where T : A<T>
{

}

public class B : A<B>
{

}

Note that A doesn't reference B, but you get to encapsulate the derived type at base level on methods etc.

2
  • I've tried instantiating your code, but it doesn't work --> A<A<B>> a = new A<A<B>>(); Jun 13, 2010 at 20:26
  • Sorry, in all places I use this little pattern the base class is abstract - you instantiate and use B, not A<B>. I've just tried A<B> b = new A<B>(); where B is derived from A, and this works. You get only the base class reference. Jun 14, 2010 at 8:06
0

When you create an instance of A, it will create an internal instance of B.... which will - guess what - create an internal instance of B which will - guess what - create an internal instance of B which will - guess what - create an internal instance of B.

This is because the B constructor goes on the A constructor which is recursive. Stack overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.