COM中的内存管理 [英] Memory management in COM

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

问题描述

在COM服务器执行期间分配一块内存是很常见的,然后通过一个输出参数将该内存块传递给客户端。然后是客户义务释放该内存,使用CoTaskMemFree()等方法。



问题是,这个内存块分配在哪里? strong>假设COM服务器和COM客户端在不同的进程中,为了客户端访问该内存块,它将 SHOULD 分配在客户端的进程地址空间中。但是是真的吗?我听说COM有一个任务内存分配器



首先,它是COM服务器分配然后,COM客户端得到那块内存,使用它,然后用CoTaskMemFree()释放它,然后在COM客户端的请求下使用CoTaskMemAlloc() )。



因此,任务内存分配器必须跟踪客户端和服务器进程。否则,它不会知道谁(服务器)做了内存分配操作和谁(客户端)应该给予该内存。



任何人都可以对这个主题发表一些看法?

解决方案

好吧,任务内存分配器是一个COM拥有的分配器,暴露这些 CoTaskMem * 函数。现在假设客户端和服务器在不同的进程中,服务器使用 CoTaskMemAlloc()分配一个out参数。它是如何得到客户端?



COM子系统与编组就是这样。服务器分配内存并从其COM方法实现中返回控制。 COM子系统现在必须将调用结果封送到客户端。它只需要拥有那些记忆,并把它记录给客户端。客户端在其(客户端)堆上分配其自己的块,将数据复制到客户端,服务器上的块被释放。



因此,客户端和服务器地址空间总是分开的,不会发生直接的数据访问。每个使用其自己的内存分配器,编组在中间启动,使客户端分配内存并使服务器空闲内存,以便客户端获得合法分配的块的所有权,并且服务器释放其自身分配的块的所有权。 p>

所以对客户端来说,几乎看起来像服务器分配了内存并将其返回给客户端。一个值得注意的例外是允许逻辑地址不同 - 例如地址为 0x10001000 的服务器分配的内存,并将该地址与块一起返回。客户端不能保证在相同的逻辑地址获取块 - 地址将取决于客户端分配器。


It is very common to allocate a block of memory during the execution of COM server, and then pass that memory block to the client through an ouput parameter. Then it is the client obligation to free that memory, with methods such as CoTaskMemFree().

Question is, where is this block of memory allocated? Supposing that the COM server and COM client are in different processes, in order for the client to access that memory block, it SHOULD be allocated in the client's process address space. But is that true? I heard that COM has a "Task Memory Allocator". But I know little about it.

Just some wild guesses:

First, it is the COM server allocate the memory at the request of the COM client, with CoTaskMemAlloc().

And then, COM client get that piece of memory, use it, and free it with CoTaskMemFree().

So the "Task Memory Allocator" must keep track of both the client and server processes. Otherewise, it won't know who(the server) did the memory allocation action and who(the client) should be given that memory. Then, the allocated memory will be somehow injected to the client's process address space.

Could anyone shed some light on this topic?

解决方案

Well, "task memory allocator" is a COM-owned allocator that exposes those CoTaskMem* functions. Now suppose the client and the server are in different processes and the server uses CoTaskMemAlloc() to allocate an "out" parameter. How does it get to the client?

COM subsytem with marshalling does that. The server allocates memory and returns control from its COM method implementation. COM subsystem now has to marshal the call results to the client. It simply takes ownership of that memory and marshals it to the client. The client allocates its own block on its (the client) heap, data is copied to the client, the block on the server is freed. The client get ownership of the block and must free it later otherwise the block is leaked.

So the client and the server address spaces are always separated and no direct data access happens. Each uses its own memory allocator, marshalling kicks in in the middle to make the client allocate memory and make the server free memory so that the client gets ownership of a legally allocated block and the server releases ownership of the block it itself allocated.

So to the client it almost looks like the server allocated the memory and returned it to the client. The one notable exception is that logical addresses are allowed to differ - say server allocated memory at address 0x10001000 and returned that address together with the block. The client is not guaranteed to get the block at the same logical address - the address will be up to the client side allocator.

这篇关于COM中的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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