Search code examples
c++heap-memorydynamic-memory-allocation

CrtIsValidHeapPointer(block) error in C++ -- deletion of a memory


Basically, in the code there is a class 'Board' including pointers called head and tail from a struct object 'slot' pointing to dynamically allocated lists which are in the property of a slot's class object (different class where it has dynamically allocated list); therefore, a linked list is created in this way. To sum up, head and tail pointers point to pointers of dynamically allocated char lists.

    struct slot 
{
    slot* next;
    slot* prev;
    CharStack slotStack; //  every slot has slotstack

};

In one point, I want to delete the the stack (dynamically allocated list) and a slot* pointer pointing to the list, then rearrange it in order not to mess up the linked list. In the below code, I iterate through the linked list and when it comes to the corresponding index I want to delete, it goes to the if expression.

    void Board::destroySlot(int index)
{
    // call ~CharStack()
    // delete slot
    // change the counts
    slot* ptr = head;
    int i = 0;
    while (ptr != NULL)
    {
        
        if (i == index)
        {
            char ch;
            while (!ptr->slotStack.isEmpty())
            {
                ptr->slotStack.pop(ch);
            }

            //  delete dynamically alocated list
            ptr->slotStack.~CharStack(); 

            if (ptr == head)
            {
                slot* temp = ptr->next;
                //delete ptr;
            
                head = temp;
                head->prev = NULL;
            }
            else if (ptr == tail)
            {
                slot* temp = ptr->prev;
                //delete ptr;
                
                tail = temp;
                tail->next = NULL;
            }
            else 
            {
                slot* tempPrev = ptr->prev; 
                slot* tempNext = ptr->next;

                // deleting the node slot
                //delete ptr; // GIVES ERROR // Deleting on another heap?

                // connecting the slots
                tempPrev->next = tempNext;
                tempNext->prev = tempPrev;
            }
            // Removing from the counts
            ch == 'x' ? xCnt -= 4 : oCnt -= 4;
            break;

        }

        ptr = ptr->next;
        i++;
    }

}

In this part, after I'm done with it, I cannot delete the ptr (not the char list, the ones with "delete ptr") , and it throws this error in visual studio.

enter image description here

No idea why this happens, I can delete the head ptr in the beginning of this function and in another functions. However, after the loop or in the loop deletion of it gives error.

EDIT: I guess if we do not do "ptr->slotStack.~CharStack();" which just deletes the stackArray, dynamically allocated char list, it does not crash. That list is just the struct "slot"s property's private property. Why would it cause crash because of that?


Solution

  • The default destructor in c++ will call the destructor of all members. So this line: ptr->slotStack.~CharStack(); is causing the destructor to get called twice which I assume is causing memory in the CharStack to get deallocated twice.

    My recommendation: delete that line, I don’t think you need it.