Milestone2.exe中0x7700C42D处的未处理异常:Microsoft C ++异常:内存位置为0x003EF5DC的std :: bad_alloc

时间:2018-03-23 21:10:58

标签: c++

ErrorState.h

#ifndef ERRORSTATE_ERRORSTATE_H
#define ERRORSTATE_ERRORSTATE_H


#include <iostream>

namespace AMA
{
    class ErrorState
    {
         char* m_message;

    public:
        explicit ErrorState(const char* errorMessage = nullptr);
        //ErrorState(const ErrorState& em) = delete;
        //ErrorState& operator=(const ErrorState& em) = delete;
        void clear();
        bool isClear() const;

        void message(const char* str);

        const char* message()const;
        virtual ~ErrorState();

    };
    std::ostream& operator<<(std::ostream&, ErrorState&);

}

#endif // !ERRORSTATE_ERRORSTATE_H

ErrorState.cpp

#include <iostream>
#include "ErrorState.h"

namespace AMA
{

    ErrorState::ErrorState(const char* errorMessage)
    {
        if (errorMessage == nullptr)
        {
            m_message = nullptr;
        }
        else
        {
            message(errorMessage);
        }
    }
    /*ErrorState::ErrorState(const ErrorState& em) 
    {


    }

    ErrorState& ErrorState::operator=(const ErrorState& em) 
    {
        strncpy(m_message, em.m_message, sizeof(m_message));
        return *this;
    }
    */
    void ErrorState::clear()
    {
        delete [] this->m_message;
        this->m_message = nullptr;
    }

    bool ErrorState::isClear() const
    {
        if (this->m_message == nullptr)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    void ErrorState::message(const char* str)
    {
        //if (isClear() == false)
        //{
        //  clear();
        //}


        this->m_message = new char[strlen(str) + 1];
        strcpy(this->m_message, str);
    }

    const char* ErrorState::message()const
    {
        return this->m_message;
    }
    ErrorState::~ErrorState()
    {
        delete[] this->m_message;
    }

    std::ostream& operator<<(std::ostream& output,  ErrorState& state)
    {
        if (!state.isClear())
        {
            output << state.message();
        }
        return output;

    }
}

Main.cpp的

#include <iostream>
#include "ErrorState.h"

using namespace std;
using namespace AMA;

int main() {
    ErrorState T("Testing Error State Module");
    ErrorState e;
    int ret = 0;
    bool ok = true;
    cout << T << endl << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
    if (!e.isClear()) ok = false;
    cout << endl;
    cout << "===========| Long Message\r";
    for (int i = 0; i < 10000000; i++) {
        if (i % 1000000 == 0) {
            cout << "*";
            cout.flush();
        }
        e.message("Some error message that is really long long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long"
            " long  long  long  long  long  long  long  long  long  long  long  long  long");
    }
    cout << '*' << endl;
    cout << e << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
    if (e.isClear()) ok = false;
    cout << endl;

    e.message("Short Message");
    cout << e << endl << e.message() << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
    if (e.isClear()) ok = false;

    e.clear();
    cout << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
    if (!e.isClear()) ok = false;
    cout << endl;

    if (ok) {
        cout << "You passed all the tests!" << endl;
    }
    else {
        cout << "You did not pass all the tests, keep working on your project!" << endl;
        ret = 1;
    }

    return ret;

}

我正在尝试创建一个错误检测有点程序。但在我的其中一个函数中,它给了我错误,我真的不知道如何解决它,因为我是新的,只是学习了。这就是我需要实现这个我无法正常工作的功能

void message(const char* str);

此函数存储str:

指向的C样式字符串的副本
  • 取消分配为先前存储的消息分配的所有内存
  • 分配存储str副本所需的动态内存(记住要为null终止符包含1个额外字节)
  • 将地址str处的字符串复制到分配的内存中。

1 个答案:

答案 0 :(得分:0)

显然,我们处于同样的境地。只是想帮助你。我在Reddit上找到了这个。所以可能会帮助你。

&#34;在你的构造函数中,如果你传入一个字符串,那么它会调用&#39; message&#39;,而第一件事是在[m_message]成员上调用delete。哪个尚未初始化为nullptr。因此,您在随机值上调用delete,在本例中为0xb754bff4。&#34;

ErrorMessage::ErrorMessage(const char* errorMessage)
{
m_message = nullptr;

if(errorMessage) 
    message(errorMessage);
}