静态类成员在构造时引发异常
Static class member throws an exception when constructed
>我有一个类,其中包含另一个类的静态成员。
福.hclass Foo
{
static DirectXRes dxres;
};
傅.cpp DirectXRes Foo::dxres; // may throw an exception
DirectXRes.h #include <SDKDDKVer.h>
#ifndef WINVER
#define WINVER 0x0A00
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0A00
#endif
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <Windowsx.h>
#pragma comment (lib, "d2d1.lib")
#pragma comment (lib, "Dwrite.lib")
#pragma comment (lib, "Windowscodecs.lib")
#pragma comment (lib, "Ole32.lib")
#pragma comment (lib, "Shlwapi.lib")
#pragma comment (lib, "Crypt32.lib")
#pragma comment (lib, "d3d11.lib")
#include <d2d1.h>
#include <d2d1_1.h>
#include <d2d1_2.h>
#include <d2d1helper.h>
#include <d2d1_2helper.h>
#include <dwrite.h>
#include <wincodec.h>
#include <d3d11_1.h>
class DirectXRes
{
DirectXRes()
: D2DFactory(nullptr),
WriteFactory(nullptr),
ImgFactory(nullptr),
D3DDevice(nullptr),
ImmediateContext(nullptr),
D2DDevice(nullptr),
DXGIAdapter(nullptr),
D2DContext(nullptr),
DriverType(D3D_DRIVER_TYPE_NULL),
FeatureLevel(D3D_FEATURE_LEVEL_11_0)
{
}
ID2D1Factory1* D2DFactory; // Direct2D factory
IDWriteFactory* WriteFactory; // DWrite factory
IWICImagingFactory* ImgFactory; // Windows Imaging Component (WIC) factory
ID3D11Device* D3DDevice; // Direct3D device
ID2D1Device* D2DDevice; // Direct2D device
ID2D1DeviceContext* D2DContext; // Direct2D device context
ID3D11DeviceContext* ImmediateContext; // Direct3D immediate context
IDXGIAdapter* DXGIAdapter; // DXGI adapter
D3D_FEATURE_LEVEL FeatureLevel; // Direct3D feature level
D3D_DRIVER_TYPE DriverType; // Direct3D driver type
};
我遇到的问题是,如果我在头文件 ( DirectXRes.h
) 之外实现构造函数,也就是说,在构造静态对象时DirectXRes.cpp
我会收到异常:
查看器.exe中0x7550DAE8 (KernelBase.dll) 处未处理的异常: 0xE0434352(参数:0x80131016、0x00000000、0x00000000、 0x00000000,0x71F80000)。
如果我将类构造函数的实现保留在头文件中,一切正常。
对此例外有什么解释吗?
我认为这里的问题是静态初始化顺序。对于静态变量(例如示例中的静态变量),标准不指定初始化它们的顺序,因此这可能会有所不同。通过将代码从头文件移动到 cpp 文件,您很可能无意中更改了Foo::dxres
相对于使用它的其他内容的初始化顺序。在一个版本中,使用它的东西在之后初始化,而在另一个版本(崩溃的版本)中,它在之前初始化,导致它访问导致崩溃的未初始化Foo::dxres
。
如果可以更改设计,我会通过静态 getter 成员函数Foo::dxres
访问。这样,可以保证在第一次想要使用它时
class Foo
{
public:
static DirectXRes& getDxres();
};
//In either the header or cpp:
DirectXRes& Foo::getDxres()
{
static DirectXRes dxres;
return dxres;
}
相关文章:
- 是否允许分配器构造和销毁成员函数从内部逻辑引发异常?
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- 异常成员类 OOP
- 在字符串类上的成员函数和out_of_range异常处理
- 在使用 In Order 遍历成员函数时引发异常(堆栈溢出)时出现问题
- C++执行期间的类成员函数错误/异常处理
- 如果从类成员初始值设定项引发的异常调用 std::terminate()
- 从成员构造函数(Brace Initializer vs Initializer列表)抛出异常
- 在 C++ 中的构造函数中引发异常时销毁对象的成员变量
- 私有成员"current"的值在成员函数结束时发生异常更改
- 异常处理和成员变量
- C++C-tor处理默认成员值构造异常
- C++模板如果匹配类型else,则调用成员函数引发异常
- constexpr静态成员变量的异常行为
- 错误 C2248:无法访问在类中声明的私有成员,编译器行为异常
- 如何从私有成员子类对象捕获异常
- 在初始化成员变量期间有条件地处理异常
- 在构建过程中捕获特定基础成员的异常
- 使用 C++ Boost 的格式对象作为异常成员
- 如果在成员中抛出异常,则删除母对象