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 8989069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:09:49+00:00 2026-06-15T22:09:49+00:00

Possible Duplicate: C++: Delete this? There is a class Foobar created on heap. I

  • 0

Possible Duplicate:
C++: Delete this?

There is a class Foobar created on heap. I want to exit application when it dies. It must die, when I call die() function. There are some private properties created on heap – I also need to delete them. I wrote that code:

Foobar::Foobar()
{
    m_var = new int(1);
}

Foobar::~Foobar()
{
    delete m_var;
    exit(0);
}

void Foobar::die()
{
    delete this;
}

The question is in delete this line. If I call it, will Foobar::~Foobar() be called, or not?

P.S. If there is better solution, suggest it, please.

  • 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-06-15T22:09:50+00:00Added an answer on June 15, 2026 at 10:09 pm

    Assuming the object is allocated dynamically with new: Yes, delete this will cause the destructor to be called. However, you should be very careful with deleting this. In particular, you need to ensure that no subsequent operations try to access any members of the class.

    Also, if this is ever done on memory which is not dynamically allocated (i.e. with new), this leads to undefined behaviour. In fact, this also leads to undefined behaviour if the object was allocated via new[].

    See this link for more information:
    http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

    Here’s another SO question about it:
    Is delete this allowed?

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

Sidebar

Related Questions

Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: Delete object and all its child objects in Entity Framework? This code:
Possible Duplicate: C++: Delete this? Object-Oriented Suicide or delete this; I wonder if the
Possible Duplicate: In vim is there a way to delete without putting text in
Possible Duplicate: C++: Delete this? I am trying to create a system to manage
Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: Is there any danger in calling free() or delete instead of delete[]?
Possible Duplicate: Delete a folder on SD card In my app i saved all
Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: Programmatically delete my own app Currently I am working on a iphone

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.