簡體   English   中英

調試斷言失敗表達式 __acrt_first_block == header

[英]Debug Assertion Failed Expression __acrt_first_block == header

我一直在試圖弄清楚為什么會發生這種情況,也許這只是由於目前缺乏經驗,但真的可以使用一些幫助。

當我運行使用 C++20 編譯成 DLL 的代碼時,我發現調試斷言失敗,表達式為 __acrt_first_block == header。

我縮小了代碼失敗的范圍,但奇怪的是,當我將 Init(std::string filePath function 簽名更改為不包含參數時,它運行得很好。代碼如下,希望有人能提供幫助。

記錄器.h

#pragma once

#include "../Core.h"

#include <memory>
#include <string>
#include "spdlog/spdlog.h"

namespace Ruby
{
    class RUBY_API Logger
    {
    public:
        static void Init(std::string filePath);

        inline static std::shared_ptr<spdlog::logger>& GetCoreLogger() { return coreLogger; }
        inline static std::shared_ptr<spdlog::logger>& GetClientLogger() { return clientLogger; }

    private:
        static std::shared_ptr<spdlog::logger> coreLogger;
        static std::shared_ptr<spdlog::logger> clientLogger;
    };
}

記錄器.cpp

namespace Ruby
{
    std::shared_ptr<spdlog::logger> Logger::coreLogger;
    std::shared_ptr<spdlog::logger> Logger::clientLogger;

    void Logger::Init(std::string filePath)
    {
        std::string pattern{ "%^[%r][%n][%l]: %v%$" };
        auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filePath, true);

        // Setup the console and file sinks
        std::vector<spdlog::sink_ptr> coreSinks;
        coreSinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
        coreSinks.push_back(fileSink);
        // Bind the sinks to the core logger.
        coreLogger = std::make_shared<spdlog::logger>("RUBY", begin(coreSinks), end(coreSinks));
        // Set the Patterns for the sinks
        coreLogger->sinks()[0]->set_pattern(pattern);
        coreLogger->sinks()[1]->set_pattern(pattern);
        // Tell spdlog to flush the file loggers on trace or worse message (can be changed if necessary).
        coreLogger->flush_on(spdlog::level::trace);
        // Set the default level of the logger
        coreLogger->set_level(spdlog::level::trace);

        // Do the same for the client logger
        std::vector<spdlog::sink_ptr> clientSinks;
        clientSinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
        clientSinks.push_back(fileSink);
        clientLogger = std::make_shared<spdlog::logger>("APP", begin(clientSinks), end(clientSinks));
        clientLogger->sinks()[0]->set_pattern(pattern);
        clientLogger->sinks()[1]->set_pattern(pattern);
        clientLogger->flush_on(spdlog::level::trace);
        clientLogger->set_level(spdlog::level::trace);
    }
}

入口點.h

#pragma once

#ifdef RB_PLATFORM_WINDOWS

extern Ruby::Application* Ruby::CreateApplication();

int main(int argc, char** argv)
{
    Ruby::Logger::Init("../Logs/Recent_Run.txt");
    RB_CORE_INFO("Initialized the logger.");

    auto app = Ruby::CreateApplication();
    app->Run();
    delete app;

    return 0;
}

#else
    #error Ruby only supports windows
#endif // RB_PLATFORM_WINDOWS

對於遇到類似問題的其他人,這是我解決它的方法。

本質上, Init() function 的 function 簽名是問題所在。 std::string參數導致調試斷言觸發,我現在最好的猜測是因為移動語義,但我仍然不確定那部分。 所以我發現有幾種方法可以解決這個問題。

方法一:

將參數設為const char* 我不太喜歡這種方法,因為它依賴於 C 樣式的字符串,如果您嘗試用現代 C++ 編寫程序,這是一個巨大的倒退。

方法二:

使參數成為const std::string& 使其成為對stringconst引用可以防止移動語義(據我所知)並且斷言不再觸發。 我更喜歡此修復程序,因為它將程序保留在現代 C++ 中。

我希望這可以幫助任何有類似問題的人,並注意靜態和移動語義。

暫無
暫無

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

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