Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6036469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:55:44+00:00 2026-05-23T05:55:44+00:00

Recently, while taking an introductory unit focused on Object Oriented programming, I was introduced

  • 0

Recently, while taking an introductory unit focused on Object Oriented programming, I was introduced to the Garbage Collector in C#, and that it’s role is to “clean up” objects that are no longer being referenced. Then I was introduced to destructors, and how they’re called just before the object is deleted.

Naturally, I got thinking, but I never remembered to ask the lecturer about it; what will happen if you create an instance of a class within the destructor of the same class?

C# example

class Person{
 ~Person(){

  Person p = new Person();
  Console.WriteLine("Person destroyed");

 }
}

class Program{
 static void Main(string[] args){
  Person p = new Person();
 }
}

I would like to approach this from a more theoretical point of view, so I’m reluctant (at this stage) to try it since I probably wouldn’t understand anyway, but I have a few theories. Besides, I’m not at my regular computer right now 😉

  • Person.~Person() is going to recurse, as each time the new Person is created, it’s going to call its destructor and create a new Person ad infinitum, or until some kind of memory-related exception occurs. Subsequently, main will never terminate.
  • The compiler will complain (adding this option to every scenario seems like a good idea anyway).
  • Somehow, some kind of “destructor skipping” will occur. ie. object destruction wouldn’t be called sequentially, so neither would the constructor.

Now for a similarly related question. If the Garbage Collector’s role is to delete the objects that are no longer referenced/needed, how would a situation like the one above be handled in an environment without a Garbage Collector – say, C++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:55:45+00:00Added an answer on May 23, 2026 at 5:55 am

    There’s no real mystery here I think.

    It won’t ‘recurse’ as such – you’re just chucking a new object on the managed heap which is immediately dereferenced; thus making it a candidate for garbage collection.

    Eventually the garbage collector will come round again, triggering the operation again etc.

    That’s not recursion – more like a chain. But ultimately each Person will be removed from memory.

    And, after a while the Garbage collector will send you an email complaining that you’re not playing fair.

    As for C++, well my guess is a stack overflow, since construction/destruction is happening there and then, and a very sulky computer afterwards.

    If your next logical thought is ‘shouldn’t the runtime/language stop this from happening?’ – no. The language or runtimes in question are not there to stop you doing something that would otherwise be considered ill-advised; it trusts you, the programmer, to make sure you’re not doing that.

    That said – in an application shutdown scenario (re your comment below) the .Net runtime is going to act out of self-interest and will ultimately stop processing these finalizers to enact a shutdown. Finalizers are for your benefit, not the runtime’s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: Recently while looking at a structured text editor I noticed they used a
Recently, my Eclipse 3.4 installation started complaining while trying to update installed software. I
We recently were given a client requirement to encrypt all their data while at
While working in a Java app, I recently needed to assemble a comma-delimited list
I've had an app doing prefix searches for a while. Recently the index size
I recently came across a ASP 1.1 web application that put a whole heap
A have a Perl cron job that recently started having its HTTPS connections start
A little while back (after recently installing VStudio 2010) I used the Go to
I have recently come across SCALA while going through the designs of twitter and
I've recently inspected the performance of a F# app and while digging through the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.