469,927 Members | 1,912 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem REVISISTED

>Hello,
Regarding Marshal.AllocHGlobal versus Marshal.AllocCoTaskMem Does anyone
know the circumstances in which one should use one over the other?

http://msdn.microsoft.com/library/de...-us/cpref/html /
frlrfsystemruntimeinteropservicesmarshalclassalloc cotaskmemtopic.asp
http://msdn.microsoft.com/library/de...-us/dllproc/ba s
e/overlapped_str.asp
Above are the relevant links to MSDN. The details are fairly sparse in
terms of useage guidance.

Thanks in advance.
-Mike

If you're passing the pointer to an API that specifically says the
memory should be allocated with CoTaskMemAlloc, then you should use
AllocCoTaskMem. In most other situations AllocHGlobal will work just
as well.

Mattias

I've been researching this a little further and it seems to me that both
functions allocate memory from the default per process heap. The only
difference is that AllocCoTaskMem (derived from IMalloc / CoTaskMemAlloc() )
is used by the COM allocator and / or when the programmer wishes to allocate
memory for a COM object. In all other scenarios (such as passing pointers
to unmanaged memory to unmanaged DLLs) then either function is ok to use.

http://msdn.microsoft.com/library/de...us/dngenlib/ht
ml/msdn_virtmm.asp

So i'm going to stick with AllocHGlobal since im not dealing with COM
objects.

Still would appreciate any further insight anyone might have.
Thanks
-Mike
Nov 21 '05 #1
0 1121

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Stefan | last post: by
4 posts views Thread by **Developer** | last post: by
2 posts views Thread by scottt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.