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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:49:32+00:00 2026-05-30T09:49:32+00:00

I am having trouble preventing objects that my pointers are pointing from being freed.

  • 0

I am having trouble preventing objects that my pointers are pointing from being freed. I think this is the problem but I do not know how to resolve it.

My code:

enum TOKEN_TYPE {
    OPEN, CLOSE, TEXT
};

struct Token {
    int type;
    std::string value;
};

typedef std::vector<Token>::iterator token_it;

Tree::Tree(token_it start) {    
    root.value = start->value;
    createNode(++start, &root);
}

void Tree::createNode(token_it it, Node* parent) {
    Node current;

    current.value = it->value;
    current.parent = parent;

    if(parent != 0) {
        parent->children.push_back(&current);
    }

    ++it;
    while(it->type != TOKEN_TYPE::CLOSE && it->value != current.value) {
        if(it->type == TOKEN_TYPE::OPEN) {
            createNode(it, &current);
        }

        ++it;
    }
}

I tried stepping through the program and everything’s perfect until the end when the program begins to exit the createNode calls where garbage collection frees current which leaves parent pointing to nothing; at least that is what I think is happening.

  • 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-30T09:49:33+00:00Added an answer on May 30, 2026 at 9:49 am

    First off, there’s no garbage collection in C++.

    Second, use smart-pointers instead of raw pointers:

    void Tree::createNode(token_it it, SmartPtr<Node> parent)
    

    Third, your assumption is right:

    {
       Node current;
       parent->children.push_back(&current);
    } //current is destroyed here
    

    This happens because current is allocated in automatic storage.

    If you manage memory inside parent, you can create the current node dynamically:

    {
       Node* current = new Node;
       parent->children.push_back(current);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble inheriting from a template class. Looks something like this: template<typename type> class
Having trouble with this..I'm sure I'm missing something simple but I simply want the
Having trouble getting this to work: /// <summary> /// Retrieve search suggestions from previous
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
Having trouble with an sql statement to retrieve a record between two timestamps. This
Having trouble getting the oauth access token and secret exchanged from the request tokens
I'm having trouble forking a long-running process from some code running under mod_perl2. Everything
having trouble coming up with search parameters for this issue, so I can't find

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.