简体   繁体   English

Win32 API 中的指针所有权

[英]Pointer Ownership in the Win32 API

I'm trying to understand how the Win32 API handles Pointer ownership.我试图了解 Win32 API 如何处理指针所有权。

The concrete example i am looking at right now is the ownership of pointers to strings that i pass to the API and that i get from the API.我现在正在查看的具体示例是指向我传递给 API 以及从 API 获取的字符串的指针的所有权。

Take for example the SetWindowText function.SetWindowText函数为例。 It takes an LPCSTR , a pointer to a null terminated widestring.它需要一个LPCSTR ,一个指向空终止宽LPCSTR的指针。

Another example would be the RegisterClass function and in conjunction the WNDCLASS struct.另一个例子是RegisterClass函数和WNDCLASS结构。 The RegisterClass function takes a pointer to a WNDCLASS struct and the WNDCLASS struct again contains a LPCSTR to the class name. RegisterClass函数采用指向WNDCLASS结构的指针,而WNDCLASS结构再次包含指向类名的LPCSTR

Does the API take ownership of that memory or do i need to handle freeing it? API 是否拥有该内存的所有权,还是我需要处理释放它?

It seems unlikely to me that it would take ownership but my knowledge of C/C++ conventions isn't good enough to say for certain, and i cant find any documentation about ownership conventions for the API.我似乎不太可能拥有所有权,但我对 C/C++ 约定的了解不足以肯定地说,而且我找不到任何关于 API 所有权约定的文档。

The general answer is: Read the documentation for the function in question.一般的答案是:阅读相关函数的文档。 If it doesn't say anything about taking ownership, then it's a good bet that it doesn't take ownership and you need to deal with the memory.如果它没有说明任何有关获得所有权的信息,那么可以打赌它没有获得所有权并且您需要处理内存。

Although it's been a while since I did much significant work at the level of the Windows API, I recall occasionally coming across functions that documented they required memory to be allocated via a particular method in first place - because that's the only way to know how to properly deallocate it.虽然我已经有一段时间没有在 Windows API 级别做了很多重要的工作,但我记得偶尔会遇到记录它们首先需要通过特定方法分配内存的函数 - 因为这是知道如何分配的唯一方法正确释放它。

And that leads to the second way to reason about this.这导致了第二种推理方式。 Imagine you were the implementer of the function.假设您是该功能的实现者。 If you were just given a raw pointer with no other information about it, could you know how the deallocate the memory it points to?如果你只是得到一个没有其他信息的原始指针,你能知道如何释放它指向的内存吗? Was that memory allocated with new ?那个内存是用new分配的吗? malloc ? malloc some other API call?其他一些 API 调用? or maybe it's on the stack?或者它在堆栈上? C / C++ / the level of the Windows API doesn't have any facility to determine how memory was allocated from just having a pointer alone. C / C++ / Windows API 的级别没有任何工具来确定仅通过一个指针来分配内存的方式。 So most functions are just not going to deal with that hassle if they don't need to.因此,如果不需要,大多数功能就不会处理这种麻烦。 And the few that have a good reason will have to follow the route of documenting their requirements.少数有充分理由的人将不得不遵循记录他们需求的途径。

Does the API take ownership of that memory or do i need to handle freeing it? API 是否拥有该内存的所有权,还是我需要处理释放它?

There is no such automatic memory management in Win32 API . Win32 API没有这种自动内存管理。 If you allocate some data in your program and give this data to Win32 API functions, you will have to free it in your program.如果在程序中分配了一些数据并将这些数据提供给Win32 API函数,则必须在程序中free它。

API in C are usually designed like that, because there is no way for the other layers to determine if the pointers are used somewhere else in the program. C 中的API通常是这样设计的,因为其他层无法确定指针是否在程序中的其他地方使用。 If manual memory management is annoying for you, you could search for garbage collectors which will manage the memory automatically for you, with a small tradeoff regarding performances.如果手动内存管理对您来说很烦人,您可以搜索garbage collectors ,它会自动为您管理内存,并在性能方面进行一些权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM