2017-10-21 137 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

這就是我所做的,我仍然得到內存泄漏 –