Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not familiar with coverity or the exact rules that it uses, but it seems that you will have a memory leak if the name of the node is already in the map. That is, if the body of your if statement is not executed, then you loose the pointer to the memory that you just allocated. Perhaps you wanted something like:</p> <pre><code>if (m_name2NodeMap.find(nodeName) == m_name2NodeMap.end()) m_name2NodeMap[nodeName] = NodePtr; else delete NodePtr; NodePtr = NULL; </code></pre> <p>Edit: since I responded almost at the same time as Daemin, let me add more details:</p> <p>As ildjarn mentioned, you also need to deallocate those objects that do end up in the map by adding a destructor:</p> <pre><code>~graph() { for( std::map&lt; std::string, node*&gt;::iterator i = m_name2NodeMap.begin(); i != m_name2NodeMap.end(); ++i ) { delete i-&gt;second; } } </code></pre> <p>For completeness, I should note that:</p> <ol> <li>The map will be deleted automatically after the destructor finishes because it's a member variable.</li> <li>The entries in the node map will be deleted automatically when the map is deleted.</li> <li>The string keys will be deleted when the entries are deleted.</li> </ol> <hr> <p>The preferred way to deal with complex object lifetimes is to use a smart pointer. For example, the <a href="http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/shared_ptr.htm" rel="nofollow">boost::shared_ptr</a> or the tr1::shared_ptr would work like this. Note: I may not have the syntax exact.</p> <pre><code>class node { ... } class graph { public : void populateGraph() { std::string nodeName = getNodeName(); boost::shared_ptr&lt; node &gt; NodePtr( new node(nodeName) ); NodePtr-&gt;setLevel(-1); if (m_name2NodeMap.find(nodeName) == m_name2NodeMap.end()) m_name2NodeMap[nodeName] = NodePtr; } .... private : std::map&lt; std::string, boost::shared_ptr&lt;node&gt; &gt; m_name2NodeMap; } }; </code></pre> <p>See how we've eliminated the destructor and explicit calls to delete? Now the node objects will be destroyed automatically just like the node names.</p> <p>On another node, you should look into the <a href="http://www.cplusplus.com/reference/stl/map/insert/" rel="nofollow">std::map::insert</a> function which should eliminate that if statement all together.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload