我有一个类,其中包含另一个类的静态成员。
Foo.hclass Foo
{
static DirectXRes dxres;
};
Foo.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
中,当构造静态对象:
Unhandled exception at 0x7550DAE8 (KernelBase.dll) in Viewer.exe: 0xE0434352 (parameters: 0x80131016, 0x00000000, 0x00000000, 0x00000000, 0x71F80000).
如果我将类构造函数的实现保留在头文件中,一切都会正常工作。
这个异常有什么解释吗?
最佳答案
我认为这里的问题是静态初始化顺序。对于静态变量(例如您示例中的静态变量),标准没有指定它们的初始化顺序,因此这可能会有所不同。通过将代码从头文件移动到 cpp 文件,您很可能无意中更改了 Foo::dxres
相对于使用它的其他内容的初始化顺序。在一个版本中,使用它的东西是在之后初始化的,而在另一种版本(崩溃的版本)中,它是在之前初始化的,导致它访问未初始化的 Foo::dxres ,从而导致崩溃。
如果可以改变设计,我会让 Foo::dxres
通过静态 getter 成员函数来访问。这样就可以保证它在第一次需要使用它时可用:
class Foo
{
public:
static DirectXRes& getDxres();
};
//In either the header or cpp:
DirectXRes& Foo::getDxres()
{
static DirectXRes dxres;
return dxres;
}
关于c++ - 静态类成员在构造时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38242615/