如何处理复制指针的释放

发布于 2024-12-04 02:38:29 字数 1708 浏览 1 评论 0原文

我正在尝试用 C++ 构建一个带有构造函数和析构函数的结构(这是一件坏事吗?我应该使用类来代替吗?),因为我不想每次都编写不必要的 10 行删除 struct.member成员应该被释放

结构体的成员大多是指向其他类型的指针。

但是,除了成员指针作为唯一指针的选项之外,我还希望能够将另一个指针的副本分配给结构中的成员指针。因此,如果我尝试在结构的析构函数内释放该内存,它可能已经被释放,从而导致崩溃。 (特别是如果复制的指针是从同一结构的另一个对象复制的)

ptr 到 ptr-types 而不是结构中的 ptr-types 也不能解决问题(我认为),因为我也想允许成员能够成为指向对象的唯一指针。

我想到的一个可能的解决方案是同时拥有一个 ptr-to-ptr 和一个由 ptr-to-ptr 指向的 ptr。但这样做效率相当低。

我最好的解决方案是什么?

我希望(但怀疑)我的问题足够清楚。

这是一些可能有帮助的示例代码。

struct GraphicsDesc
{
public:
ID3D11VertexShader*         pSolidColorVS;
ID3D11PixelShader*          pSolidColorPS;
ID3D11InputLayout*          pInputLayout;
ID3D11ShaderResourceView*   pColorMap;
ID3D11SamplerState*         pSampler;
ID3D11BlendState*           pBlendState;
ID3D11Buffer*               pVertexBuffer;
UINT                        VertexSize;
D3D_PRIMITIVE_TOPOLOGY      Topology;

GraphicsDesc() 
{
    pSolidColorVS   = nullptr;
    pSolidColorPS   = nullptr;
    pInputLayout    = nullptr;
    pColorMap       = nullptr;
    pSampler        = nullptr;
    pBlendState     = nullptr;
    pVertexBuffer   = nullptr;
    VertexSize      = 0;
    Topology        = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
}

virtual ~GraphicsDesc() 
{
    if (pVertexBuffer) {
        pVertexBuffer->Release();
    }
    if (pBlendState) {
        pBlendState->Release();
    }
    if (pSampler) {
        pSampler->Release();
    }
    if (pColorMap) {
        pColorMap->Release();
    }
    if (pInputLayout) {
        pInputLayout->Release();
    }
    if (pSolidColorPS) {
        pSolidColorPS->Release();
    }
    if (pSolidColorVS) {
        pSolidColorVS->Release();
    }
}
};

I'm trying to build a struct with a constructor and destructor in C++ (is that a bad thing to do? Should I use a class instead?), because I would hate to write unnecessary 10 lines of delete struct.member every time the members should be deallocated

The members of the struct are mostly pointers to other types.

However, apart from the option of the member pointer being a unique pointer, I also want to be able to assign a copy of another pointer to a member pointer in the struct. Consequently, if I try to deallocate that memory inside the destructor of the struct, it could have already been deallocated, causing a crash. (especially if the pointer copied was from another object of the same struct)

ptr to ptr-types instead of ptr-types in the struct wouldn't solve the problem either (I think), since I also want to allow the member to be able to be a unique pointer to an object.

A possible solution that I've thought of, is to have both a ptr-to-ptr and a ptr to be pointed to by the ptr-to-ptr. But that would just be rather inefficient.

What would my best solution be?

I hope (but doubt) my question is clear enough.

Here's some example code that might help.

struct GraphicsDesc
{
public:
ID3D11VertexShader*         pSolidColorVS;
ID3D11PixelShader*          pSolidColorPS;
ID3D11InputLayout*          pInputLayout;
ID3D11ShaderResourceView*   pColorMap;
ID3D11SamplerState*         pSampler;
ID3D11BlendState*           pBlendState;
ID3D11Buffer*               pVertexBuffer;
UINT                        VertexSize;
D3D_PRIMITIVE_TOPOLOGY      Topology;

GraphicsDesc() 
{
    pSolidColorVS   = nullptr;
    pSolidColorPS   = nullptr;
    pInputLayout    = nullptr;
    pColorMap       = nullptr;
    pSampler        = nullptr;
    pBlendState     = nullptr;
    pVertexBuffer   = nullptr;
    VertexSize      = 0;
    Topology        = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
}

virtual ~GraphicsDesc() 
{
    if (pVertexBuffer) {
        pVertexBuffer->Release();
    }
    if (pBlendState) {
        pBlendState->Release();
    }
    if (pSampler) {
        pSampler->Release();
    }
    if (pColorMap) {
        pColorMap->Release();
    }
    if (pInputLayout) {
        pInputLayout->Release();
    }
    if (pSolidColorPS) {
        pSolidColorPS->Release();
    }
    if (pSolidColorVS) {
        pSolidColorVS->Release();
    }
}
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2024-12-11 02:38:29

所有这些指针数据成员都是指向引用计数的 COM 类型的指针。 Release 不一定会销毁所指向的对象。它会减少引用计数,并且仅在没有更多引用时才销毁该对象。

您应该使用 CComPtrCComQIPtr,而不是使用原始指针并自己调用 AddRefRelease,它们会自动执行引用计数。

All of those pointer data members are pointers to COM types that are reference counted. Release does not necessarily destroy the pointed-to object. It decrements the reference count and only destroys the object if there are no more references to it.

Rather than using raw pointers and calling AddRef and Release yourself, you should use CComPtr and CComQIPtr, which automate the reference counting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文