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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:23:43+00:00 2026-06-17T07:23:43+00:00

This is a made-up scenario based off a real issue. If I have a

  • 0

This is a made-up scenario based off a real issue. If I have a base class:

class Vehicle
{
public:
     void run() { incrementStuff(); }
     virtual void incrementStuff() { } 
};

And a derived class:

class Car : public Vehicle
{
public:
     Car() : Vehicle() { stuff = new StuffClass(); }
     ~Car()  { delete stuff; stuff = NULL; }

     virtual void incrementStuff() { if (stuff != NULL) stuff->increment(); } 

protected:
     StuffClass* stuff;
};

Then say I have a thread which calls Vehicle::run() periodically. I have another thread that eventually deletes the pointer to car. The order of destruction will cause car to be deleted first, then vehicle.

What happens if the thread (living in the Vehicle object) calls the incrementStuff function AFTER the destructor has run in car (but obviously before vehicle has been destructed)? Will the Car::incrementStuff be executed and try to dereference an already deleted pointer? Or will the Vehicle::incrementStuff be called which is safe and does nothing?

Assume that the thread cannot call into Car::incrementStuff while Car::~Car() is running (this is prevented by a mutex).

This code has been refactored to avoid this, but I was just wondering if anyone can shed some light onto how this works, or if it is just plain undefined behavior?

More generally, what happens if I try to call Car::incrementStuff after Car has been destructed but before Vehicle has been? Will the NULL check work, or is that memory already deallocated and could be used by anything now? Or is that memory not ‘freed’ until the base object has been destructed?

  • 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-17T07:23:44+00:00Added an answer on June 17, 2026 at 7:23 am

    You’re over thinking it. If you dereference a pointer which has already been deleteed you invoke undefined behavior. That’s all you really need to know. As soon as you call delete ptr; ptr is invalid.

    Synchronize access to this object.

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

Sidebar

Related Questions

My scenario is this - I have two ASP.net websites. Both sites run on
hi I have a scenario like this I made a chat program where a
Let us say we have the following scenario: class MyModel < ActiveRecord::Base after_save :throw_after_save
I have this tricky scenario and want so advice to you. My client have
consider the following scenario: I have a storyboard-based app. I add a ViewController object
I have a scenario where the base project consists of java code and web
Consider this scenario. We have an internal Rails 2 app that connects to a
Here's the setup (using a made-up scenario), but applicable to my real situation: For
I made this function to verify a user's twitter credentials. Its running on two
I made this bookmarklet: javascript:(function(){var s=document.createElement('script');s.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');document.getElementsByTagName('body')[0].appendChild(s);$('#hldIntMain').hide();$('#fadeBackground').hide();return false;})() Formatted code: // Add in jQuery var

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.