2017-10-21 164 views
1

我试图删除C++中的链接列表中的节点,但它一直说在删除事务后_pending_tx不是nullptr。此外Valgrind的说我有内存泄漏,我无法弄清楚内存泄漏删除节点C++

bool BankData::void_transaction(const size_t& customer_id, const size_t& timestamp) { 

bool var8 = false; 

if (transaction_exists(customer_id, timestamp) == true) 
{ 
    int blah3 = customerVector.size(); 
    for (int i = 0; i < blah3; i++) 
    { 
     if (customerVector.at(i)._customer_id == customer_id) 
     { 
      if (customerVector.at(i)._pending_txs != nullptr) 
      { 
       CSE250::dTransactionNode *temp = customerVector.at(i)._pending_txs; 
       while (temp->_tx._timestamp != timestamp) 
       { 
        temp = temp->_next; 
        if ((temp->_next == nullptr) && (temp->_tx._timestamp != timestamp)) 
        { 
         var8 = false; 
        } 
       } 
       if (temp->_tx._timestamp == timestamp) 
       { 
        if ((temp->_prev == nullptr) && (temp->_next == nullptr))   //head and only node 
        { 
         delete customerVector.at(i)._pending_txs; 
         customerVector.at(i)._pending_txs = nullptr; //after i delete the head and only node i reset it to a nullptr 
         var8 = true; 
        } 
        else if ((temp->_prev == nullptr) && (temp->_next != nullptr))  //head 
        { 
         temp = customerVector.at(i)._pending_txs->_next; 
         delete customerVector.at(i)._pending_txs; 
         customerVector.at(i)._pending_txs = temp; 
         customerVector.at(i)._pending_txs->_prev = nullptr; 
         var8 = true; 
        } 
        else if ((temp->_prev != nullptr) && (temp->_next == nullptr)) //tail 
        { 
         temp = customerVector.at(i)._pending_txs->_prev; 
         delete customerVector.at(i)._pending_txs; 
         customerVector.at(i)._pending_txs = temp; 
         customerVector.at(i)._pending_txs->_next = nullptr; 
         var8 = true; 
        } 
        else                //middle node 
        { 
         temp = customerVector.at(i)._pending_txs->_next; 
         customerVector.at(i)._pending_txs->_next->_prev = customerVector.at(i)._pending_txs->_prev; 
         delete customerVector.at(i)._pending_txs; 
         customerVector.at(i)._pending_txs = temp; 
         //temp->_prev->_next = temp->_next; 
         //temp->_next->_prev = temp->_prev; 
         //temp = nullptr; 
         //delete temp; 
         var8 = true; 
        } 
       } 
      } 
     } 
    } 
} 
return var8; 

这是我试图删除的节点结构:

namespace CSE250 { 
struct dTransactionNode;} 

struct CSE250::dTransactionNode { 
Transaction _tx; 

dTransactionNode* _prev; 

dTransactionNode* _next; 

dTransactionNode(size_t time, double amount) : _tx(time, amount), _prev(nullptr), _next(nullptr) { }}; 

我也想不出为什么当我尝试删除它时,它只会删除时间戳,而不是时间戳和金额。所以当我运行我的事务存在函数时,它仍然表示存在一部分事务。

That's the valgrind message

And those are the error messages I get that say its not pointing to a nullptr after the transaction has been voided even thoguh i set it manually to a null ptr

+0

删除将空指针设置为空。 – 2017-10-21 17:55:09

+1

使用合适的智能指针。 –

+0

你会说 temp = customerVector.at(i)._ pending_txs; customerVector.at(i)._ pending_txs = nullptr; 删除温度: ? –

回答

1

当你delete东西,指针没有被设置为自动nullptr。这是程序员的职责。阅读更多信息Why doesn't delete set the pointer to NULL?

当您将指针设置为空时,它不会删除内存。如果你这样做,那么在你拨打delete之前,如果你没有用另一个指向它的指针删除内存,那么你将创建一个内存泄漏。

智能指针可以使用,这对你来说是诀窍。

+0

没错,第二条线没有照顾? delete customerVector.at(i)._ pending_txs; customerVector.at(i)._ pending_txs = nullptr; //在删除头并且只有节点后,我将它重置为nullptr var8 = true; –

+0

是@MatthewKirshy。 – gsamaras

+0

这就是我所做的,我仍然得到内存泄漏 –