Re: Which to use, AllocCoTaskMem or AllocHGlobal



**Developer**,
| Will I know that I need COM memory?
Yes! At least I would hope one would, by being familiar with the API that
you are calling to know if it requires the COM allocator or not.

Looking at the DocumentProperties API I do not see that any of its
parameters require the COM allocator, ergo I would not use AllocCoTaskMem, I
would use AllocHGlobal instead. My choice to use AllocHGlobal of course may
be wrong as I quickly looked at the DocumentProperties API on MSDN just now.
For example I know the Structured Storage API & MAPI "requires" COM
allocator, however I don't see where on MSDN that Structured Storage
requires the COM allocator... MAPI has its own API calls for memory that I
understand uses an IMalloc.

When reviewing AllocCoTaskMem & AllocHGlobal it helps to understand the
CoTaskMemAlloc API:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/c4cb588d-9482-4f90-a92e-75b604540d5c.asp

And GlobalAlloc (LocalAlloc):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalalloc.asp

My understanding is that AllocCoTaskMem (aka CoTaskMemAlloc) will allocate
memory via the standard implementation of IMalloc. What I don't know is
where CoTaskMemAlloc allocates its memory out of (if its the "directly" the
same "pool" as GlobalAlloc (if it simply calls GlobalAlloc) or if it
allocates a Heap & allocates from that (HeapCreate & HeapAlloc APIs).

| Will I know that I need COM memory?
I think the question might really be: does it hurt calling AllocCoTaskMem
instead of AllocHGlobal? I suspect calling AllocCoTaskMem when you don't
need to is not as bad as not calling AllocCoTaskMem when you do need to. (if
that made any sense ;-))

Or: can FreeHGlobal (GlobalFree API) really safely be used interchangable
with FreeCoTaskMem (CoTaskMemFree API)? If you are allocating & freeing the
memory it really doesn't matter; if you are allocating the memory & someone
else frees it then I would say it does matter.

I would expect Adam Nathan's book to address this, however I'm not seeing it
in his book right now.


Hope this helps
Jay

" **Developer**" <REMOVEdeveloper@xxxxxxxxxx> wrote in message
news:Of$nN6VgFHA.2644@xxxxxxxxxxxxxxxxxxxxxxx
| I've seen a lot of examples that use AllocCoTaskMem (see below)
|
| Will I know that I need COM memory? What I mean is I will be obvious donig
| "COM" things not simply calling some Windows API and not knowing I'm
dealing
| with COM. Somehow, I wonder about the FORMATRANGE example becaues I know
| the RichTextBox windows control uses COM at times.
|
| Thanks again
|
|
|
|
| 'Allocate memory for the FORMATRANGE struct and
|
| 'copy the contents of our struct to this memory
|
| Dim lParam As IntPtr
|
| lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(lFr))
|
| Marshal.StructureToPtr(lFr, lParam, False)
|
|
|
|
|
|
|
| Dim lPointerToDevMode As IntPtr = Marshal.AllocCoTaskMem(lDevModeDataSize)
|
| lRet = WinSpool.DocumentProperties(IntPtr.Zero, lPrinterHandle,
printerName,
| lPointerToDevMode, IntPtr.Zero, GDI.DM_OUT_BUFFER)
|
| If (lRet < 0) Then
|
| MsgBox("Cannot get the DEVMODE structure.")
|
| GoTo cleanup
|
| End If
|
|
|
|
|
|
|
| lPointerToDevMode = Marshal.AllocCoTaskMem(lSizeOfDevMode)
|
| lFlag = WinSpool.DocumentProperties(IntPtr.Zero, lPrinterHandle,
| printerName, lPointerToDevMode, IntPtr.Zero, GDI.DM_OUT_BUFFER)
|
|
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxx> wrote in message
| news:OaWhQDSgFHA.3512@xxxxxxxxxxxxxxxxxxxxxxx
| > **Developer**,
| > AllocCoTaskMem is used to allocate COM memory, unless I knew I needed to
| > allocate COM memory (memory that another COM object/API needed to
release)
| > I
| > would use AllocHGlobal.
| >
| > In other words I use AllocHGlobal, as I have yet needed to use
| > AllocCoTaskMem.
| >
| > Hope this helps
| > Jay
| >
| > " **Developer**" <REMOVEdeveloper@xxxxxxxxxx> wrote in message
| > news:uwAWFQ0fFHA.1960@xxxxxxxxxxxxxxxxxxxxxxx
| > | If I need to allocate memory, maybe because I'm going to:
| > | Marshal.StructureToPtr
| > | Does it matter which of the following I use?
| > |
| > | Marshal.AllocCoTaskMem
| > | Marshal.AllocHGlobal
| > | I know the arguments are different which would make one more convient
in
| > a
| > | given situation, but other that that does it make any signifficant
| > | difference.
| > |
| > |
| > |
| > | Thanks
| > |
| > |
| > |
| > |
| >
| >
|
|


.



Relevant Pages

  • Re: CE6.0 Driver Pointer Marshalling - passing pointers out only?
    ... VirtualAllocEx can be used to allocate memory in a specified process. ... Read/WriteProcessMemory can be used to access this memory. ... structures that relate to API parameters. ... Use VirtualCopyEx to map a region of kernel memory to the client's ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Working Set = Virtual Size, is it possible?
    ... Yes I guess that this is an option, this API ... to allocate non paged memory. ... Allocate address space with VirtualAlloc(... ... So far I've looked at SetProcessWorkingSetSize and VirtualLock. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Working Set = Virtual Size, is it possible?
    ... One option is to use the "Address Windowing Extensions" API to allocate your ... Allocate address space with VirtualAlloc(... ... Allocate physical pages of memory with AllocateUserPhysicalPages ... So far I've looked at SetProcessWorkingSetSize and VirtualLock. ...
    (microsoft.public.win32.programmer.kernel)
  • Tru64 issues with Infinite limits
    ... An automated test in my nightly build was failing due to Out of Memory ... Cannot allocate block 146 ...
    (comp.unix.tru64)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)

Loading