gpt4 book ai didi

C++ 抛出未知异常类型,但仅在生产构建中

转载 作者:行者123 更新时间:2023-11-28 04:55:40 25 4
gpt4 key购买 nike

我是 C++ 的新手,一直在做一个小项目,但遇到了一些绊脚石。我有一个指针映射,我需要能够将存储的指针转换为适当的子类指针。这是导致问题的代码段:

std::map<int, noise::module::Module *> moduleInstance;

// ...

// This is a valid id set earlier
std::cout << id << std::endl;

// This is the same address as an instance of noise::module::Constant
// created earlier
std::cout << moduleInstance[id] << std::endl;

// This works, and is a value set by the subclass, so it exists and is
// being instantiated correctly.
std::cout << moduleInstance[id]->sourceModuleCount << std::endl;

noise::module::Constant *constantModule;

try {
constantModule = dynamic_cast<noise::module::Constant *>(moduleInstance[id]);
} catch(const std::runtime_error& re) {
std::cout << "Runtime error: " << re.what() << std::endl;
} catch (const std::exception& ex) {
std::cout << "Error occurred: " << ex.what() << std::endl;
} catch (...) {
std::cout << "Unknown error" << std::endl;
}

// This is a random address unless built with --debug
std::cout << constantModule << std::endl;

// This also works fine with --debug
if (constantModule == nullptr)
{
std::string err = "Module id '" + std::to_string(id) + "' is not an instance of ConstantModule.";
Nan::ThrowReferenceError(Nan::New(err).ToLocalChecked());
return;
}

根据我读过的内容,如果有问题,这应该通过将指针设置为空指针来失败。但是,当我运行生产构建时,它会打印出 Unknown error,并且无法恢复。如果我使用 --debug 标志进行构建,它(显然)可以完美地工作。

我将如何调试它?到目前为止,我什至无法告诉 什么 错误被抛出,更不用说为什么了。即使只是能够找到错误的名称也是一个有用的起点。


编辑以添加(非)工作示例:

主.cc

#include <assert.h>
#include <iostream>
#include <map>
#include <nan.h>

using std::cout;
using std::endl;

class SuperClass
{
public:
SuperClass()
{
}

virtual int virtualMethod() = 0;
};

class SubClassA : public SuperClass
{
public:
SubClassA() : SuperClass()
{
}

int virtualMethod()
{
return 3;
}
};

class SubClassB : public SuperClass
{
public:
SubClassB() : SuperClass()
{
}

int virtualMethod()
{
return 4;
}
};

std::map<int, SuperClass *> instanceMap;

void Run(const Nan::FunctionCallbackInfo<v8::Value> &info)
{
SubClassA *subClassAInstance = new SubClassA();
SubClassB *subClassBInstance = new SubClassB();

instanceMap[0] = subClassAInstance;
instanceMap[1] = subClassBInstance;

SubClassB *subClassPtr;

try {
subClassPtr = dynamic_cast<SubClassB *>(instanceMap[1]);
} catch (...) {
cout << "Unknown error" << endl;
return;
}

if (subClassPtr == nullptr)
{
cout << "Not an instance of SubClassB" << endl;
}
else
{
assert(subClassPtr->virtualMethod() == 4);
cout << "Addon done" << endl;
}
}

void Init(v8::Local<v8::Object> exports)
{
exports->Set(
Nan::New("run").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(Run)->GetFunction());
}

NODE_MODULE(addon, Init)

绑定(bind).gyp

{
"targets": [
{
"target_name": "addon",
"sources": [
"./main.cc"
],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}

主要.js

const addon = require('bindings')('addon');

addon.run();

console.log('JS Done');

设置

npm init -y
npm i --save bindings nan
node-gyp configure

运行

node-gyp rebuild && node ./main

最佳答案

正如 Mark 在评论中所注意到的,RTTI 在 node-gyp 构建环境中被关闭。此外,似乎(至少在 Windows 上)您无法使用 binding.gyp 覆盖该设置。修复是编辑 C:\Users\<user>\.node-gyp\<version>\include\node\common.gypi直接通过设置 'RuntimeTypeInfo''true''target_defaults' -> 'configurations' -> 'Release' -> 'msvs_settings' -> 'VCCLCompilerTool' 下.

这不是一个很好的解决方案,所以我重构了代码以便能够使用 static_cast而是通过为每个子类设置具有唯一值的类型属性。

关于C++ 抛出未知异常类型,但仅在生产构建中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com