如何释放C ++ WinRT值结构的内存 [英] How to free memory of c++ WinRT value structs
问题描述
我是否必须以及如何从Windows运行时组件中创建的值结构中释放内存,该值结构已返回到托管C#项目中?
Do I have to, and how do I, free memory from a value struct created in a Windows Runtime Component that has been returned to a managed C# project?
我声明了结构
// Custom struct
public value struct PlayerData
{
Platform::String^ Name;
int Number;
double ScoringAverage;
};
喜欢
auto playerdata = PlayerData();
playerdata.Name = ref new String("Bla");
return playerdata;
我是新来的释放内存的人,却不知道如何以及何时释放它. 任何人?
I'm new with freeing memory and haven't got a clue how and when to free this. Anyone?
推荐答案
将值结构分配给另一个变量时,将其成员进行复制,以便两个变量都被复制拥有自己的数据副本(请参见值类和结构(C ++/CX)).从函数返回值结构时,同样的规则适用.
When a value struct is assigned to another variable, its members are copied, so that both variables have their own copy of the data (see Value classes and structs (C++/CX)). The same rule applies, when returning a value struct from a function.
在代码中,您具有playerdata
,这是类型为PlayerData
的对象,具有自动存储持续时间. return
语句复制playerdata
(包括Platform::String^
成员),并将此副本返回给调用方.之后,playerdata
超出范围,并被自动销毁.
In your code you have playerdata
, an object of type PlayerData
with automatic storage duration. The return
statement makes a copy of playerdata
(including the Platform::String^
member), and returns this copy to the caller. After that, playerdata
goes out of scope, and is automatically destroyed.
换句话说:您发布的代码可以正常工作.您不必显式释放任何内存.
In other words: The code you posted works as expected. You do not have to explicitly free any memory.
这篇关于如何释放C ++ WinRT值结构的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!