【问题标题】:I get the Assertion failure "file_name != nullptr" but only in release mode我得到断言失败“file_name!= nullptr”但仅在发布模式下
【发布时间】:2018-12-16 14:14:17
【问题描述】:

当我尝试在调试模式下运行代码时,一切正常,但是当我尝试在发布模式下运行它时,我收到错误“file_name!= nullptr”(错误来自 fopen)。我知道错误的含义,但我不知道为什么会收到错误以及如何修复它

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
    String imageName("C:/Users/x/Desktop/pic.png"); // by default
    if (argc > 1)
    {
        imageName = argv[1];
    }

    Mat image;
    image = imread(imageName, IMREAD_COLOR); // Read the file

    if (image.empty())                      // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
    imshow("Display window", image);                // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

错误信息:

>-----------Microsoft Visual C++ Runtime Library----------------
>
>
>Debug Assertion Assertion Faild!
>
>Programm: C:\Users\x\...\myProgramm.exe
>File: minkernel\crts\ucrt\src\appcrt\stdio\fopen.cpp
>
>Line: 30
>
>Expression: file_name != nullptr

【问题讨论】:

  • 应该是字符串,不是字符串,不是吗?同样在 MSVC 命令行参数中,IDE 界面中的 Release/Debug 参数是不同的。
  • 字符串不起作用。我不使用命令行参数,我使用默认的 imageName 值
  • Windows 中的路径分隔符是反斜杠。您正在使用正斜杠。这可能有效,也可能无效。
  • 我用反斜杠试过了,但它不起作用。
  • @Trifik 用反斜杠替换时,需要加两个来转义反斜杠C:\\Users\\x\\Desktop\\pic.png(只需确认已完成)

标签: c++ windows visual-c++ c++17


【解决方案1】:

您必须确保在调试和发布模式下使用正确的依赖项(.lib 文件)。

【讨论】:

  • @LightnessRacesinOrbit 许多 .lib 文件的名称末尾有一个“d”,代表 debud。在我的情况下,“opencv_world400d.lib”用于调试模式,“opencv_world400.lib”用于发布模式
  • "仅声明未初始化或未定义" 不正确,即定义且字符串默认初始化
  • “字符串获取的是一个 NULL 指针 (nullptr),因为它没有定义” 也不正确。这是一个空但有效的字符串。
  • 要么使用调试而不是发布(反之亦然)引入了 UB,或者在调试中你会看到一个故意和想要的诊断,它在发布中默默地隐藏起来(因此你想要修复错误),但如果它只是试图捕捉一个空字符串,诊断措辞很奇怪
猜你喜欢
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多