簡體   English   中英

Milestone2.exe中0x7700C42D的未處理異常:Microsoft C ++異常:內存位置0x003EF5DC的std :: bad_alloc

[英]Unhandled exception at 0x7700C42D in Milestone2.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003EF5DC

錯誤狀態

#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副本所需的動態內存(請記住為空終止符包括1個額外的字節)
  • 將地址str處的字符串復制到分配的內存中。

顯然,我們處於同樣的情況。 只是想幫助您。 我在Reddit上發現了這個。 因此可能會為您提供幫助。

“在您的構造函數中,如果您傳入一個字符串,則它將調用'message',首先發生的是在[m_message]成員上調用delete。該成員尚未初始化為nullptr。因此,您在一個隨機值,在這種情況下為0xb754bff4。”

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

if(errorMessage) 
    message(errorMessage);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM