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

  • Home
  • SEARCH
  • 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 8787601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:55:53+00:00 2026-06-13T21:55:53+00:00

Suppose, I have the following code: class Data { private: int *m_arr; int m_size;

  • 0

Suppose, I have the following code:

class Data
{
  private:
    int *m_arr;
    int m_size;
    bool m_deAlloc;
  public:
    Data(int *arr, int size): m_arr(arr), m_size(size), m_deAlloc(false) {}
    ~Data(){ if(m_deAlloc) delete[] m_arr; }
...
};

void foo(Data &d)
{
   // uses d ...
}  

void foo_wrapper(int *arr, int size)
{
   Data d(arr, size); // should I create it as new Data(arr, size)?

   foo(d); // asynchronous call...

} // ~Data() will be called but m_arr array will not be erased...

int main()
{
  int *arr = new int[...];
  ...
  foo_wrapper(arr,...); // internally calls foo asynchronously...
  ...
  // wait for foo execution completion....
  ...
  delete[] arr;
}

I tried it with gcc and it works apparently but I think it’s not a valid program as “Data” reference passed to “foo” from “foo_wrapper” can be invalid as destructor of the object passed might get called before foo has finished executing (asynchronous execution). Although I don’t delete data (m_arr) but still what happens with the object reference when destructor gets called?

Does the C++ (gcc) compiler just calls the destructor. For example when a destrutor for object “d” is called, does it relocate memory allocation for object “d” and set “d” to some invalid reference?

  • 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-13T21:55:54+00:00Added an answer on June 13, 2026 at 9:55 pm

    In this case, the Data instance (d) you pass to foo() will just be placed on the main thread’s stack — it can certainly be destructed and out of scope (e.g. potentially reused as the main thread continues execution) before foo() returns. It is undefined behavior. Even if the allocation were not overwritten, you should not play with a destructed object.

    One way or another, you need to ensure the parameter d is valid and not destructed until after foo() has returned.

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

Sidebar

Related Questions

Suppose I have following code package memoryleak; public class MemoryLeak { public static int
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
Say suppose I have the following Java code. public class Example { public static
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Suppose I have the following: class dataClass { public: int someData; float moreData; void
Suppose I have the following code: class a { public: virtual void do_a() =
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
I have the following code public class BaseController : Controller { SqlConnection con; DataSet

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.