2

Im trying to delete a node in a linked list in c++ but it keeps saying that _pending_tx is not nullptr after deleting transaction. Also valgrind says I have a memory leak and I can't figure it out

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;

This is the node struct that I'm trying to delete:

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) { }};

I also can't figure out why when i try to delete it, it will only delete the timestamp and not the timestamp and amount. so when I run my transaction exists function it still says part of the transaction exists.

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

1 Answers1

1

When you delete something, the pointer is not set to nullptr automatically. It's the programmers duty to do so. Read more in Why doesn't delete set the pointer to NULL?

When you set a pointer to null, it doesn't delete the memory. If you do so, before you call delete, then you will create a memory leak, if you don't delete the memory with another pointer that refers to it.

Smart pointers can be used, which do the trick for you.

gsamaras
  • 71,951
  • 46
  • 188
  • 305