C++ 抛出未知异常类型,但仅在生产构建中
C++ throws an unknown exception type, but only in production build
我是 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
标志进行构建,它(显然)可以完美地工作。
我该如何调试呢?到目前为止,我什至无法告诉 抛出什么 错误,更不用说 为什么 。即使只是能够找到错误的名称也是一个有用的起点。
编辑以添加(非)工作示例:
main.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)
binding.gyp
{
"targets": [
{
"target_name": "addon",
"sources": [
"./main.cc"
],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
main.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 覆盖该设置。解决方法是通过在 'target_defaults' -> 'configurations' -> 'Release' -> 'msvs_settings' -> 'VCCLCompilerTool'
.
下将 'RuntimeTypeInfo'
设置为 'true'
来直接编辑 C:\Users\<user>\.node-gyp\<version>\include\node\common.gypi
这不是一个很好的解决方案,因此我重构了代码,以便能够使用 static_cast
,而不是通过为每个子类设置类型 属性 具有唯一值。
我是 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
标志进行构建,它(显然)可以完美地工作。
我该如何调试呢?到目前为止,我什至无法告诉 抛出什么 错误,更不用说 为什么 。即使只是能够找到错误的名称也是一个有用的起点。
编辑以添加(非)工作示例:
main.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)
binding.gyp
{
"targets": [
{
"target_name": "addon",
"sources": [
"./main.cc"
],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
main.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 覆盖该设置。解决方法是通过在 'target_defaults' -> 'configurations' -> 'Release' -> 'msvs_settings' -> 'VCCLCompilerTool'
.
'RuntimeTypeInfo'
设置为 'true'
来直接编辑 C:\Users\<user>\.node-gyp\<version>\include\node\common.gypi
这不是一个很好的解决方案,因此我重构了代码,以便能够使用 static_cast
,而不是通过为每个子类设置类型 属性 具有唯一值。