What is the difference between COM heap and CRT heap?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: What is the difference between COM heap and CRT heap?

  1. #1
    Join Date
    Jul 2005
    Posts
    894

    What is the difference between COM heap and CRT heap?

    I assume CRT heap is the heap when we call new operator. COM heap is the heap when we call SysAllocString. I wonder if there is any difference? Thanks.

  2. #2
    Join Date
    Jun 2010
    Posts
    50

    Re: What is the difference between COM heap and CRT heap?

    :Hi threre,
    There is a cake, one person eats with a spoon, the other with his hand. The cake will be finished some time later

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,765

    Re: What is the difference between COM heap and CRT heap?

    they're different pools... that server different purposes.

    SysAllocString should be used for BSTR allocation only.

    Don't allocate with new and free with the SysFree...() function
    and don't allocate with SysAlloc...() and free with delete.

  4. #4
    Join Date
    Jul 2005
    Posts
    894

    Re: What is the difference between COM heap and CRT heap?

    Can I call new to allocate the memory for BSTR? If not, why not? Thanks.
    Quote Originally Posted by OReubens View Post
    they're different pools... that server different purposes.

    SysAllocString should be used for BSTR allocation only.

    Don't allocate with new and free with the SysFree...() function
    and don't allocate with SysAlloc...() and free with delete.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,284

    Re: What is the difference between COM heap and CRT heap?

    Quote Originally Posted by LarryChen View Post
    Can I call new to allocate the memory for BSTR? If not, why not? Thanks.
    Well, it is ONLY my guess and I may be wrong:

    Because you don't know how this memory must be allocated and filled in.
    And a memory block allocated with new won't be accessed from any other process.
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 1999
    Posts
    27,423

    Re: What is the difference between COM heap and CRT heap?

    Quote Originally Posted by LarryChen View Post
    Can I call new to allocate the memory for BSTR? If not, why not?
    The same reason why any API would give you a special allocation/deallocation functions to use instead of your own.

    You don't know how the memory is to be allocated, you don't know how the memory is to be deallocated, and you don't know if there is special housekeeping must be done for these types after the memory is allocated.

    You don't even know if memory really is allocated (it could all be a giant memory pool by COM, and SysAllocString just "allocates" memory by giving you a chunk of this pool).

    Regards,

    Paul McKenzie

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: What is the difference between COM heap and CRT heap?

    Quote Originally Posted by VictorN View Post
    Well, it is ONLY my guess and I may be wrong:

    Because you don't know how this memory must be allocated and filled in.
    Well, you know how it will be filled in (BSTR is documented), but there are subtleties, such as the fact that the BSTR pointer does not point to the beginning of allocated memory.

    Basically, even if it is possible to hack the BSTR using new, there's no good reason to pursue such an option.

  8. #8
    Join Date
    Jul 2005
    Posts
    894

    Re: What is the difference between COM heap and CRT heap?

    I don't quite get your points. Let me put it this way. When you use new operator to allocate the memory, you DO know how the memory is to be allocated? Besides, what do you mean by "special housekeeping"? Thanks.
    Quote Originally Posted by Paul McKenzie View Post
    The same reason why any API would give you a special allocation/deallocation functions to use instead of your own.

    You don't know how the memory is to be allocated, you don't know how the memory is to be deallocated, and you don't know if there is special housekeeping must be done for these types after the memory is allocated.

    You don't even know if memory really is allocated (it could all be a giant memory pool by COM, and SysAllocString just "allocates" memory by giving you a chunk of this pool).

    Regards,

    Paul McKenzie

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,284

    Re: What is the difference between COM heap and CRT heap?

    Quote Originally Posted by Lindley View Post
    Well, you know how it will be filled in (BSTR is documented),
    Perhaps.
    However, the piece of code of SysAllocString I could find refers to the CoTaskMemAlloc function, which is not much clear...
    Victor Nijegorodov

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: What is the difference between COM heap and CRT heap?

    Quote Originally Posted by LarryChen View Post
    I don't quite get your points.
    Paul's point is simple: If a type tells you to use a certain function to allocate it, then you do it. Trying to do something different without a very good reason is a waste of time and effort. Paul just detailed a few reasons why difficulties might arise, but it is by no means a comprehensive list.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,423

    Re: What is the difference between COM heap and CRT heap?

    Quote Originally Posted by LarryChen View Post
    I don't quite get your points. Let me put it this way. When you use new operator to allocate the memory, you DO know how the memory is to be allocated?
    There are many API's across the board that serve different purposes, graphics, math, sockets, etc. In a lot of these libraries, you are asked to "allocate" a certain type or types. The API gives you the function to use to allocate. There is a reason for this, so let me ask you, what do youu think is this reason? Do you think the authors created these allocation functions because they had nothing else better to do with their time, or is there another reason?
    Besides, what do you mean by "special housekeeping"? Thanks.
    What if SysAllocString does much more internally than just allocate memory? You bypass all of this when you use "new", probably making the internal BSTR mechanism of reference counting invalid.

    If you see many factory patterns, you are not allowed to use "new" to create your own objects from the factory -- you ask the factory for the new object, and the factory is responsible for creating them and giving them to you. This keeps creation under total control of the factory and not someone's "create these objects any way I feel like" code. The BSTR loosely follows this paradigm.

    If the objects are your objects, or the types are regular C++ types, then "new" comes to play. If the type is one defined by a library, and the library's documenation states clearly that to "create object x, use this function", then you follow the documentation. Again, there is a reason for this, and the author of the library knows why this is, much better than a user of the library who has no knowledge of the library's internals.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 5th, 2010 at 03:12 PM.

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,927

    Re: What is the difference between COM heap and CRT heap?

    I assume CRT heap is the heap when we call new operator. COM heap is the heap when we call SysAllocString. I wonder if there is any difference?
    CRT heap is the heap where you allocate with CRT allocation functions like malloc, calloc, etc. COM heap (never heard the term) must be the heap where you allocate with CoTaskMemAlloc. The difference is obvious: different allocation/deallocation functions used. Though deep inside both possibly appear based on the same HeapAlloc/HeapFree functionality.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center