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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:36:47+00:00 2026-05-29T12:36:47+00:00

I enjoy using the operators new and delete in C++ a lot but often

  • 0

I enjoy using the operators new and delete in C++ a lot but often have a problem calling delete later on in the program’s code.

For example, in the following code:

class Foo {
public:
    string *ace;
    Foo();
    ~Foo();
};
Foo::Foo() {
    ace = new string;
}
Foo::~Foo() {
    delete ace;
}
void UI::ButtonPressed() { //Happens when an event is triggered
    Foo *foo = new Foo;
    ui->label = ace; //Set some text on the GUI
    delete foo; //Calls the destructor, deleting "ace" and removing it from the GUI window
}

I can declare a new string but when I delete it, it removes the value from the GUI form because that string has now been deleted.

Is there a way for me to delete this allocated string somehow later on?

I don’t want to declare it as a global variable and then delete it on the last line of the program’s source code. I could just never call delete but from what I have been taught that’s bad and results in memory leaks.

  • 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-29T12:36:50+00:00Added an answer on May 29, 2026 at 12:36 pm

    You should read about the RAII pattern. It is one of the most important concepts to know for a C++ programmer.

    The basic idea is that the lifetime of a resource (a new’ed object, an HTTP connection, etc.) is tied to the lifetime of an object. This is necessary in order to write exception safe code.

    In your case, the UI widget would make a copy of the object and free it in its own destructor. The calling code could then free its copy right away (in another destructor).

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

Sidebar

Related Questions

I enjoy developing algorithms using the STL, however, I have this recurring problem where
I have a very strange problem in my code. I'm using Ogre and I'm
Greets- We gots a few nutters in work who enjoy using while(true) { //Code
I have just recently discovered how much I enjoy developing code the TDD way:
I've begun using ReSharper and really enjoy it. However I have a project which
I've got Xcode 3.2.1, and enjoy using it, but when I'm editing a file
I've been using LINQ for awhile (and enjoy it), but it feels like I
I have been using Prism for a while now and enjoy how much easier
I use both ruby on rails and Java. I really enjoy using migrations when
I am not a mathematician. I enjoy a good math puzzle, but I admit

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.