dotnetstep dotnetstep - 14 days ago 5
C# Question

Exception throw from constructor and what happen to instance and memory allocation?

I have class like this. ( This is just example)

public class NewTest
{
public int I { get; set; }
public NewTest()
{
I = 10;
throw new ApplicationException("Not Possible");
}
}


Now if I use class like this

NewTest t = new NewTest();


In above line as NewTest constructor throw exception variable t never assign any value as before constuctor get complete it throw exception but as per test and also per other question ( Why throwing exception in constructor results in a null reference?) object is created.

Now this object is created in Heap but it does not hold any root variable for reference so does it create problem for garbage collection ? or Is it memory leak of something ?




Another Example

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
NewMethod();
System.GC.Collect();
Console.WriteLine("Completed");
Console.ReadLine();
}

private static void NewMethod()
{
Object obj = null;
try
{
Console.WriteLine("Press any key to continue");
Console.ReadLine();
NewTest t = new NewTest(out obj);
}
catch
{
Console.WriteLine("Exception thrown");
}

try
{
Console.WriteLine("Press any key to continue");
Console.ReadLine();
NewTest1 t = new NewTest1();
}
catch
{
Console.WriteLine("Exception thrown");
}

Console.WriteLine("Press any key to continue");
Console.ReadLine();

System.GC.Collect();

Console.WriteLine("Press any key to continue");
Console.ReadLine();

}
}

public class NewTest1
{
public int I { get; set; }
public NewTest1()
{
I = 10;
throw new ApplicationException("Not Possible");
}
}

public class NewTest
{
public int I { get; set; }
public NewTest(out Object obj)
{
obj = this;
I = 10;
throw new ApplicationException("Not Possible");
}
}
}

Answer

Throwing exceptions in the constructor is not a problem to garbage collector, and it definitely does not result in memory leaks.

Although the memory allocated from the heap never makes it to your program, it is available to the internal implementation of the operator new, which takes care of ensuring that the newly created instance becomes eligible for garbage collection.

Comments