静态类成员在构造时引发异常

Static class member throws an exception when constructed

本文关键字:异常 成员 静态类      更新时间:2023-10-16

>我有一个类,其中包含另一个类的静态成员。

福.h
class 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;
}