-
August 5th, 2010, 12:17 AM
#1
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.
-
August 5th, 2010, 06:54 AM
#2
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
-
August 5th, 2010, 07:03 AM
#3
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.
-
August 5th, 2010, 07:34 AM
#4
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.
Originally Posted by OReubens
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.
-
August 5th, 2010, 10:11 AM
#5
Re: What is the difference between COM heap and CRT heap?
Originally Posted by LarryChen
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
-
August 5th, 2010, 11:53 AM
#6
Re: What is the difference between COM heap and CRT heap?
Originally Posted by LarryChen
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
-
August 5th, 2010, 12:04 PM
#7
Re: What is the difference between COM heap and CRT heap?
Originally Posted by VictorN
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.
-
August 5th, 2010, 12:13 PM
#8
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.
Originally Posted by Paul McKenzie
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
-
August 5th, 2010, 12:14 PM
#9
Re: What is the difference between COM heap and CRT heap?
Originally Posted by Lindley
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
-
August 5th, 2010, 12:39 PM
#10
Re: What is the difference between COM heap and CRT heap?
Originally Posted by LarryChen
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.
-
August 5th, 2010, 01:50 PM
#11
Re: What is the difference between COM heap and CRT heap?
Originally Posted by LarryChen
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.
-
August 6th, 2010, 01:48 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|