在C++和C#之间使用指向结构体的指针时,需要删除指针吗?

问题描述 投票:0回答:0

我有一个使用 C# 和 C++ 的程序。 C++ 用于执行低级操作,例如渲染。 在 C# 中,我正在创建一个输入类。它使用 GLFW 获取鼠标位置:

extern "C" __declspec(dllexport) Vector2* GetCursorPos()
{
    double xpos, ypos;
    glfwGetCursorPos(Window, &xpos, &ypos);

    Vector2* pos = new Vector2{ (float)xpos, (float)ypos };
    return pos;
}

这是 Vector2 结构:

struct Vector2
{
    float X;
    float Y;
};

在输入类中:

[DllImport("Internal.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe Vector2* GetCursorPos();

public static unsafe (float x, float y) GetMousePosition()
{
    Vector2* pos = GetCursorPos();
    return (pos->X, pos->Y);
}

我的问题是,我应该删除这个

Vector2*
,(以及在哪里删除)还是由于 C# 中的垃圾回收不需要它。

c# c++ dllimport dllexport
© www.soinside.com 2019 - 2024. All rights reserved.