CoTaskMemAlloc的用法? [英] Usage of CoTaskMemAlloc?

查看:526
本文介绍了CoTaskMemAlloc的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候适合使用CoTaskMemAlloc?有人可以举个例子吗?

When is it appropriate to use CoTaskMemAlloc? Can someone give an example?

推荐答案

Gosh,我不得不考虑一会儿 - 相当数量的小规模COM编程与ATL,很少不得不使用它。

Gosh, I had to think for a while for this one -- I've done a fair amount of small-scale COM programming with ATL and rarely have had to use it.

有一种情况,但是想起来: Windows Shell扩展。如果你正在处理一组文件系统对象,你可能需要处理 PIDLs (指向ID列表的指针)。这些是奇怪的小文件系统对象抽象,他们需要使用一个COM可识别的分配器,如 CoTaskMemAlloc 显式分配/释放。还有一种替代方法, SHGetMalloc (已弃用)或 CoGetMalloc 获取它只是一个抽象层使用,所以你的代码不绑定到一个特定的内存分配器,并可以使用任何适当的。

There is one situation though that comes to mind: Windows Shell extensions. If you are dealing with a set of filesystem objects you might have to deal with PIDLs (pointer to an ID list). These are bizarre little filesystem object abstractions and they need to be explicitly allocated/deallocated using a COM-aware allocator such as CoTaskMemAlloc. There is also an alternative, the IMalloc interface pointer obtained from SHGetMalloc (deprecated) or CoGetMalloc -- it's just an abstraction layer to use, so that your code isn't tied to a specific memory allocator and can use any appropriate one.

使用 CoTaskMemAlloc IMalloc 而不是 malloc()释放需要是COM-aware,以便其分配和释放在运行时一致地执行,即使分配和释放是由完全不相关的代码完成的(例如Windows分配内存,将其传输到您的C ++代码后来取消分配,或你的C ++代码分配,将其传递给别人的VB代码,然后释放它)。 malloc() new 都无法与系统的运行时堆进行交互操作,因此无法使用它们分配内存以传输到其他COM对象,或从其他COM对象接收内存并释放。

The point of using CoTaskMemAlloc or IMalloc rather than malloc() is that the memory allocation/deallocation needs to be something that is "COM-aware" so that its allocation and deallocation are performed consistently at run-time, even if the allocation and deallocation are done by completely unrelated code (e.g. Windows allocates memory, transfers it to your C++ code which later deallocates, or your C++ code allocates, transfers it to someone else's VB code which later deallocates). Neither malloc() nor new are capable of interoperating with the system's run-time heap so you can't use them to allocate memory to transfer to other COM objects, or to receive memory from other COM objects and deallocate.

这篇关于CoTaskMemAlloc的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆