C++ 堆损坏但仅在单元测试项目中

C++ Heap Corruption but only in Unit Test Project

我正在使用模板实现自定义堆栈。但是我 运行 遇到了一个问题,让我质疑内存安全性。析构函数在正常使用时工作正常,但在单元测试时会触发堆损坏。特别是在期望抛出异常的测试上。

所有堆栈代码。

#pragma once
#include "WhosebugException.h"
#include "StackEmptyException.h"

template <class T>
class Stack
{
public:
    const static int MAX_SIZE = 1000;

    Stack() : m_array(new T[MAX_SIZE]), m_size(0), m_top(0) {};
    Stack(const Stack<T>& _other)
    {
        m_array = new T[MAX_SIZE];
        for(auto i = 0; i < _other.size(); ++i)
        {
            m_array[i] = _other.m_array[i];
        }
        m_size = _other.size();
        m_top = _other.m_top;
    }

    ~Stack()
    {
        delete[] m_array;
    }

    void push(T _item)
    {
        if (m_size + 1 == MAX_SIZE + 1)
        {
            throw WhosebugException();
        }
        m_array[++m_top] = _item;
        ++m_size;
    }

    T pop()
    {
        if(m_top != 0)
        {
            --m_size;
            T item =  m_array[m_top];
            --m_top;
            return item;
        }
        throw StackEmptyException();
    }

    T peek() const
    {
        if (m_size == 0) throw StackEmptyException();
        T item = m_array[m_top];
        return item;
    }

    bool empty() const
    {
        return m_size == 0;
    }

    size_t size() const
    {
        return m_size;
    }
private:
    T* m_array;
    size_t m_size;
    int m_top;
};

导致堆损坏的单元测试。


        TEST_METHOD(CustomStackPopException)
        {
            Stack<int> stack;
            Assert::ExpectException<StackEmptyException>([&] { stack.pop(); });
        }

        TEST_METHOD(CustomStackPeekException)
        {
            Stack<int> stack;
            Assert::ExpectException<StackEmptyException>([&] { int a = stack.peek(); });
        }

        TEST_METHOD(CustomWhosebugException)
        {
            Stack<int> stack;
            const auto functor = [&]
            {
                for(auto i = 0; i <= Stack<int>::MAX_SIZE; ++i)
                {
                    stack.push(i);
                }
            };
            Assert::ExpectException<WhosebugException>(functor);
        }

当它到达第一个测试时,它会抛出一个弹出警告说:

HEAP CORRUPTION DETECTED: after Normal block(#279) at 0x0000020298CBC60.
CRT detected that the application wrote to memory after end of heap buffer.

我试过在堆上创建堆栈对象,但这导致了同样的错误。

这个成员函数

void push(T _item)
{
    if (m_size + 1 == MAX_SIZE + 1)
    {
        throw WhosebugException();
    }
    m_array[++m_top] = _item;
    ++m_size;
}

已经不正确了。假设 MAX_SIZE 等于 1,m_size 等于 0。在这种情况下,m_size + 1 不等于 MAX_SIZE + 1,并且有 writtem

    m_array[1] = _item;

在分配的数组之外。

编辑:按以下方式更改方法后

  void push(T _item)
    {
        if (m_size + 1 > MAX_SIZE)
        {
            throw WhosebugException();
        }
        m_array[++m_top] = _item;
        ++m_size;
    }

在我指出它的问题后,实际上该方法也有同样的问题。其实你什么都没拿

而且你的堆栈没有填充索引为 0 的项目。所以在构造函数中

Stack(const Stack<T>& _other)
{
    m_array = new T[MAX_SIZE];
    for(auto i = 0; i < _other.size(); ++i)
    {
        m_array[i] = _other.m_array[i];
    }
    m_size = _other.size();
    m_top = _other.m_top;
}

在位置 0 处访问了具有不确定值的项目

成员函数push应该这样定义

void push( const T &_item)
{
    if (m_size == MAX_SIZE)
    {
        throw WhosebugException();
    }
    m_array[m_top++] = _item;
    ++m_size;
}

其他成员函数根据成员函数push进行更改

使用分配号

HEAP CORRUPTION DETECTED: after Normal block(#279) at 0x0000020298CBC60.
CRT detected that the application wrote to memory after end of heap buffer.

然后在启动的某处插入

_CrtSetBreakAlloc(279);

这将向您显示内存的分配位置,并且应该给出有关损坏位置的重要提示。

为什么要自己动手?

不要自己实施stack

STL 已经有了那个容器

template <class T>
using my_stack = std::stack< T, std::vector<T> >;

感谢网友的配合,问题已经解决。是push函数出错了

推送功能已更改自:

  void push(T _item)
    {
        if (m_size + 1 == MAX_SIZE + 1)
        {
            throw WhosebugException();
        }
        m_array[++m_top] = _item;
        ++m_size;
    }

收件人:

    void push(T _item)
    {
        if (m_size == MAX_SIZE)
        {
            throw WhosebugException();
        }
        m_array[m_size] = _item;
        ++m_size;
        ++m_top;
    }

构造函数已更改自:

    Stack() : m_array(new T[MAX_SIZE]), m_size(0), m_top(0) {};

    Stack() : m_array(new T[MAX_SIZE]), m_size(0), m_top(-1) {};

现在size和top不一样了,m_top实际上指向栈顶