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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:10:08+00:00 2026-06-17T22:10:08+00:00

Edit: To anyone reading this question for future use: the error had not to

  • 0

Edit: To anyone reading this question for future use: the error had not to do with unique_ptr by any means. It was simply, as JoergB justly says in his answer, a mistake on my part to forget a virtual destructor for a base class.


After the occasional run-time crash, I figured my code was suffering from a severe case of memory-leak-itis. I ran my program with Valgrind and the doctor seems to agree: bytes are definitely lost. I can’t for the life of me figure out where it goes wrong, though.

I managed to pin the leak down into happening in these three lines:

std::unique_ptr<Operator> pointer(new Operator{"left", "right"});
NodeSpace space; // The node space takes ownership over the operator

// When I comment out the following line, Valgrind reports nothing:
space.setNode("key", move(pointer));

In the first line a unique_pointer is created, holding an instance of the Operator class. The Operator internally looks like this:

class Operator : public Node {
public:
    Operator(std::initializer_list<std::string> input_keys) {
        input_nodes_.reserve(input_keys.size());
        for_each(begin(input_keys), end(input_keys), [this](const string& key) {
            input_nodes_[key] = nullptr;
        });
    }

    // ...

private:
    std::unordered_map<std::string, Node*> input_nodes_;
};

I pass in the unique_ptr r-value reference to the following function:

void NodeSpace::setNode(const std::string& key, std::unique_ptr<Node> node);

Because the node space takes over ownership of the passed-in node, it takes a unique_ptr by value (move semantics). Internally it stores the pointer in an std::map, but even if the function body is commented out, the memory leak still happens (which leads me to believe the problem is the node argument of the function call).

Anyone know remotely where the problem could be?

Sidenote: I’m not using shared_ptr, because the nodes can refer to each other in a cyclic manner. weak_ptr could be an option, but by construction of the system it is impossible for a node to exist when its owning nodespace does not anymore.

Valgrind output:

==83791== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 606 of 794
==83791==    at 0x100060ABD: malloc (vg_replace_malloc.c:274)
==83791==    by 0x1000C9147: operator new(unsigned long) (in /usr/lib/libc++.1.dylib)
==83791==    by 0x10000CB0F: std::__1::__hash_table<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*>, std::__1::__unordered_map_hasher<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, true>, std::__1::__unordered_map_equal<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*, std::__1::equal_to<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, true>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*> > >::__rehash(unsigned long) (in ./test/mimi)
==83791==    by 0x10000C684: std::__1::__hash_table<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*>, std::__1::__unordered_map_hasher<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, true>, std::__1::__unordered_map_equal<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*, std::__1::equal_to<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, true>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mimi::Node*> > >::rehash(unsigned long) (in ./test/mimi)
==83791==    by 0x100005D5A: mimi::Operator::Operator(std::initializer_list<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >) (in ./test/mimi)
==83791==    by 0x100005984: mimi::Operator::Operator(std::initializer_list<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >) (in ./test/mimi)
==83791==    by 0x10002E940: main (in ./test/mimi)

I also don’t get why Valgrind seems to tell me the leak happens in the Operator constructor, even though the operator has already been constructed by the time NodeSpace::setNode() is called.

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

    You don’t show us key pieces of the code – the declaration of Node, in particular its destructor, parts of NodeSpace, in particular how it deletes Nodes, etc. But from your comment “Neither Node nor Operator have any destructor and copy/move constructor/assignment-operator”, it appears that that is the problem.

    If you maintain ownership of an Operator through a Node * or a unique_ptr<Node>, i.e. if you delete any derived objects through a Node *, Node must have a virtual destructor. If you don’t declare and define one, it doesn’t.

    While the resulting behavior is undefined, the outcome typically is, that destructors of members of the derived class are not called. That matches your error message.

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

Sidebar

Related Questions

[Edit #3] - to anyone reading this question: do not under any circumstance use
Edit: A note to anyone reading this, the whole reason it didn't work for
Does anyone have any suggestions on how to edit an <a href=''> link in
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
I was reading though this other question which has some really good regex's for
EDIT: Upon re-reading my original question I realized very quickly that it was very
That's not that great of a title for the question, so if anyone else
edit: On odd change that someone is reading this, I'd like to add one
I'm not sure if this is an appropriate question to ask, since it is
Does anyone have any idea of why this could happen? I have a C

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.