模板化出列无效指针:删除失败 class

Templated Dequeue invalid pointer: Fails on deleting class

我的模板化队列的出列函数对字符串队列工作正常,但如果我使用我的自定义机器人 class,它会在尝试删除指针时崩溃。我很好奇为什么。

例如,在main.cpp

#include <iostream>
#include <cstdlib>
#include <cstring>
#include "robotqueue.h"
#include "robotcustomer.h"
#include "servicestation.h"

using namespace std;

int main()
{
    //- TEST ONE: QUEUE<STRING> -//
    RobotQueue < string > stringQueue;
    string a("Tim");
    string b("Greg");

    stringQueue.enqueue(a);
    stringQueue.enqueue(b);
    stringQueue.dequeue();
    stringQueue.dequeue();

    //- TEST TWO: QUEUE<RobotCustomer> -//
    RobotQueue < RobotCustomer > robotQueue;
    RobotCustomer e("Tim",3);
    RobotCustomer f("Greg",5);

    robotQueue.enqueue(e);
    robotQueue.enqueue(f);
    robotQueue.dequeue();            <--- Segfault occurs here
    robotQueue.dequeue();
    return 0;
}

字符串队列工作正常,但出现此错误:

***Error in `q': munmap_chunk(): invalid pointer: 0x0000000001d6c108 ***
Aborted (core dumped)

我的模板队列看起来像这样(不知道你是否需要更多)。

robotqueue.hpp

// Default Constructor
template <typename T>
RobotQueue<T>::RobotQueue()
{
    m_size = 0;
    m_front = NULL;
    m_back = NULL;
}
// Default destructor
template <typename T>
RobotQueue<T>::~RobotQueue() 
{
    Node<T>* currNode = m_front, *nextNode = NULL;
    while ( currNode != NULL )
    {
        nextNode = currNode->m_next;
        delete currNode;
        currNode = nextNode;
    }
    m_size = 0;
}

template <typename T>
void RobotQueue<T>::enqueue(const T& x)
{
    Node<T> *newNode = new Node<T>;
    newNode->m_data = x;
    newNode->m_next = NULL;
    if(m_front == NULL)
        m_front = newNode;
    else
        m_back->m_next = newNode;
    m_back = newNode;
    m_size++;                           // Increments queue size
    return;
}

template <typename T>
void RobotQueue<T>::dequeue()
{
    Node<T>* tempNode = new Node<T>;
    if(m_front == NULL)
        cout << "dequeue error: Queue is empty" << endl;
    else
    {
        tempNode = m_front;
        m_front = m_front->m_next;
        delete tempNode;     <-- Segfault occurs here in RobotCustomer class
        m_size--;                       // Increments queue size
    }
    return;
}

我假设它与 RobotCustomer 是一个 class 所以 m_data 不能指向它或其他什么有关?这里不是专家 :p

RobotCustomer.h

/* ------------------  Class RobotCustomer ------------------ */
class RobotCustomer
{
private:
  string m_name;                // Name of Robot
  string* m_reqServices;        // Array of services requeseted
  int m_numServices;            // Number of services requested
  int m_currService;            // Number of services compelted
  bool m_busy;                  // Logic for if robot is in line/servicing
  bool m_done;                  // Logic for if robot is done
public:
  //- functions and such that I don't think affect the queue -//

感谢您的宝贵时间:)

---------------------UPDATED_WITH CONSTRUCTORS/DECONSTRUCTORS--------------------

RobotCustomer.cpp

// Default Constructor
RobotCustomer::RobotCustomer()
{
    m_name = "";
    m_numServices = 0;
    m_currService = 0;
    m_busy = false;
    m_done = false;
}
// Overloaded Constructor
RobotCustomer::RobotCustomer(string n, int x)
{
   m_name = n;
   m_numServices = x;
   m_reqServices = new string[m_numServices]; 
   m_currService = 0;
   m_busy = false;
   m_done = false;
}

// Default Destructor
RobotCustomer::~RobotCustomer()
{
    delete m_reqServices;
}

让我们看一下函数中的几行:

Node<T>* tempNode = new Node<T>;
tempNode = m_front;
delete tempNode;

首先分配内存并将其指针分配给 tempNode 变量。然后你重新分配那个变量指向其他内存,失去原来的指针。然后你尝试释放内存,这已经不是你原来分配的内存了。

这应该不会导致崩溃(据我所知),但它是内存泄漏。


猜测 导致崩溃的原因是您可能在 RobotCustomer 构造函数中为 m_reqServices 成员动态分配内存。但是,如果您不实现复制构造函数或复制赋值运算符,或者只是在这些函数中对指针进行浅表复制,则使用赋值

newNode->m_data = x

enqueue函数中你将有两个对象具有相同的指针,当其中一个被删除时,它的析构函数删除分配的内存,留下另一个对象指针现在指向未分配的内存,当您尝试再次释放它时会导致 未定义的行为

您需要阅读有关 the rules of three, five and zero. My recommendation is to use std::vector or std::array (as applicable) and simply follow the rule of zero 的内容。

当然,谈论使用 standard containers makes me wonder why you don't use std::queue 开头?