Expression: null pointer cannot point to a block of non-zero size

visual studio 2022

jsoncpp

GitHub - open-source-parsers/jsoncpp: A C++ library for interacting with JSON.

根据官方github下推荐给visual studio得安装方式 安装jsoncpp

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install jsoncpp

正常情况下,使用jsoncpp没有任何问题

特定情况下,原因未知,我是封装再cef3中使用出现了这个提示:

Debug Assertion Failed!

Program: ...m-113.0.5672.93_windows32\tests\cefclient\Debug\cefclient.exe
File: D:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.36.32532\include\xmemory
Line: 944

Expression: null pointer cannot point to a block of non-zero size

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

google了一大堆,没找到解决方案,后来看了官方的例子

jsoncpp/readFromString.cpp at master · open-source-parsers/jsoncpp · GitHub

换了一种写法解决了,

原来代码,这个是报错代码

  Json::Reader reader;//json解析
  Json::Value value;//表示一个json格式的对象

  std::string aaa = "{\"status\":true}";
 


  if (reader.parse(aaa.c_str(),  value) && !value.empty()) {
      if (!value["status"].asBool()) {
          return 1;
      }
  }
  else {
      return 1;
  }

改进后

  Json::Reader reader;//json解析
  Json::Value value;//表示一个json格式的对象

  std::string aaa = "{\"status\":true}";
  const auto rawJsonLength = static_cast<int>(aaa.length());


  if (reader.parse(aaa.c_str(), aaa.c_str() + rawJsonLength, value) && !value.empty()) {
      if (!value["status"].asBool()) {
          return 1;
      }
  }
  else {
      return 1;
  }

没有报错,可以跑通了

这个错误信息通常出现在C++编程中,特别是使用lambda表达式时。让我来解释一下这个错误的意思以及如何解决它: "non-local lambda expression cannot have a capture-default"这个错误表明你正在尝试在非局部作用域(通常是全局作用域或命名空间作用域)中定义一个lambda表达式,而这个lambda表达式使用了默认捕获(默认捕获可以是[=]或[&])。 具体来说,这个错误包含以下几个要点: 1. 非局部lambda表达式:指的是在全局作用域或命名空间作用域中定义的lambda表达式,而不是在函数内部定义的。 2. 捕获默认:指的是使用[=]或[&]来捕获外部变量。 3. 限制:在C++中,非局部lambda表达式不允许使用默认捕获。这是为了避免潜在的内存管理问题和生命周期问题。 解决方法: 1. 显式捕获:如果你确实需要在非局部lambda中使用外部变量,你应该显式地列出你需要的变量,而不是使用默认捕获。 2. 避免使用外部变量:如果可能的话,重新设计你的代码以避免在非局部lambda中使用外部变量。 3. 使用std::function和std::bind:如果你的设计需要更复杂的函数对象,考虑使用std::function和std::bind来创建可调用的对象。 示例修改: ```cpp // 错误的例子 auto bad_lambda = [&]() { /* ... */ }; // 错误:非局部lambda不能有默认捕获 // 正确的例子 int x = 10; auto good_lambda = [x]() { /* 使用x但不捕获其他变量 */ }; // 正确:显式捕获x ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值