后缀树c ++中的内存泄漏

时间:2014-03-01 05:22:43

标签: c++ memory memory-management memory-leaks valgrind

我使用了来自source的图书馆streed2006.cpp。删除边缘时代码存在内存泄漏。我使用以下代码清除了哈希表中的边数:

//throwing away the edges from hashtable
for(int t=0;t<HASH_TABLE_SIZE;t++)
{
    Edges[t].Remove();
    Edges[t].start_node == -1

}

valgrind输出:

3,920 bytes in 245 blocks are definitely lost in loss record 9 of 12
==6301==    at 0x4029F34: operator new(unsigned int) (in /usr/lib/valgrind   /vgpreload_memcheck-x86-linux.so)
==6301==    by 0x804A683: Edge::SplitEdge(Suffix&) (suffix_tree.cpp:555)
==6301==    by 0x804B02F: AddPrefix(Suffix&, int) (suffix_tree.cpp:753)

请指导我如何删除边缘。

能够消除内存泄漏。以下是解决方案:

 void AddPrefix( Suffix &active, int last_char_index )
{
 int parent_node;
 int last_parent_node = -1;

for ( ; ; ) {
    Edge edge;
    parent_node = active.origin_node;
    if ( active.Explicit() ) {
        edge = Edge::Find( active.origin_node, T[ last_char_index ] );
        if ( edge.start_node != -1 )
            break;
    } else { //implicit node, a little more complicated
        edge = Edge::Find( active.origin_node, T[ active.first_char_index ] );
        int span = active.last_char_index - active.first_char_index;
        if ( T[ edge.first_char_index + span + 1 ] == T[ last_char_index ] )
            break;
        parent_node = edge.SplitEdge( active );
    }

    Edge *new_edge = new Edge( last_char_index, T.N, parent_node );
    new_edge->Insert();
    //cout << "Created edge to new leaf: " << *new_edge << "\n";
    AddSuffixLink( last_parent_node, parent_node );
    if ( active.origin_node == 0 ) {
        //cout << "Can't follow suffix link, I'm at the root\n";
        active.first_char_index++;
    } else {
    /*
        cout << "Following suffix link from node "
             << active.origin_node
             << " to node "
             << Suffix_Nodes[ active.origin_node ].suffix_node
             << ".\n";
    */
        active.origin_node = Suffix_Nodes[ active.origin_node ].suffix_node;
        //cout << "New prefix : " << active << "\n";
    }
    active.Canonize();
delete(new_edge);
new_edge = NULL;
}
AddSuffixLink( last_parent_node, parent_node );
active.last_char_index++;  //Now the endpoint is the next active point
active.Canonize();
};

int Edge::SplitEdge( Suffix &s )
{
//cout << "Splitting edge: " << *this << "\n";
Remove();
Edge *new_edge =
  new Edge( first_char_index,
            first_char_index + s.last_char_index - s.first_char_index,
            s.origin_node );
new_edge->Insert();
Suffix_Nodes[ new_edge->end_node ].suffix_node = s.origin_node;
first_char_index += s.last_char_index - s.first_char_index + 1;
start_node = new_edge->end_node;
Insert();
//cout << "New edge: " << *new_edge << "\n";
//cout << "Old edge: " << *this << "\n";
delete(new_edge);
//return new_edge->end_node;
return(start_node);
}

1 个答案:

答案 0 :(得分:0)

 void AddPrefix( Suffix &active, int last_char_index )
{
 int parent_node;
 int last_parent_node = -1;

for ( ; ; ) {
    Edge edge;
    parent_node = active.origin_node;
    if ( active.Explicit() ) {
        edge = Edge::Find( active.origin_node, T[ last_char_index ] );
        if ( edge.start_node != -1 )
            break;
    } else { //implicit node, a little more complicated
        edge = Edge::Find( active.origin_node, T[ active.first_char_index ] );
        int span = active.last_char_index - active.first_char_index;
        if ( T[ edge.first_char_index + span + 1 ] == T[ last_char_index ] )
            break;
        parent_node = edge.SplitEdge( active );
    }

    Edge *new_edge = new Edge( last_char_index, T.N, parent_node );
    new_edge->Insert();
    //cout << "Created edge to new leaf: " << *new_edge << "\n";
    AddSuffixLink( last_parent_node, parent_node );
    if ( active.origin_node == 0 ) {
        //cout << "Can't follow suffix link, I'm at the root\n";
        active.first_char_index++;
    } else {
    /*
        cout << "Following suffix link from node "
             << active.origin_node
             << " to node "
             << Suffix_Nodes[ active.origin_node ].suffix_node
             << ".\n";
    */
        active.origin_node = Suffix_Nodes[ active.origin_node ].suffix_node;
        //cout << "New prefix : " << active << "\n";
    }
    active.Canonize();
//ADDED THIS DELETE HERE
delete(new_edge);
new_edge = NULL;
}
AddSuffixLink( last_parent_node, parent_node );
active.last_char_index++;  //Now the endpoint is the next active point
active.Canonize();
};

int Edge::SplitEdge( Suffix &s )
{
//cout << "Splitting edge: " << *this << "\n";
Remove();
Edge *new_edge =
  new Edge( first_char_index,
            first_char_index + s.last_char_index - s.first_char_index,
            s.origin_node );
new_edge->Insert();
Suffix_Nodes[ new_edge->end_node ].suffix_node = s.origin_node;
first_char_index += s.last_char_index - s.first_char_index + 1;
start_node = new_edge->end_node;
Insert();
//cout << "New edge: " << *new_edge << "\n";
//cout << "Old edge: " << *this << "\n";
//ADDED THIS DELETE HERE
delete(new_edge);
//return new_edge->end_node;
return(start_node);
}