Memeory allocation in COM Components and Clinet programs

Hi,

This is related to allocation/deallocation of memory in the COM Component(server) and client application.

In COM programming a situation may arise when you allocate memory in the client, pass it to the server where its freed and reallocated or visa versa. In such situations since the clinet  and server objects can be in different machine and hence in different address spaces, we cannot use malloc/free instead COM library functions CoTaskMemAlloc/CoTaskMemFree has to be used. My question is :
Can I use these COM lib functions throught my client and server code for all my dynamic memory requirement other than the cases where its a must(as mentioned in the above paragraph)totally discarding malloc/free. This way I can have one consistant way of handling memory.

Is using CoTaskMemAlloc/CoTaskMemFree instead of malloc/free for the local dynamic memory requirement affects the performance ?

Hope I am clear..

Thank you
FidoDido
fidodidoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Answers2000Connect With a Mentor Commented:
Responding again Prashanth

See you round
0
 
Answers2000Commented:
1. You can use them throughout, it's just memory.  

2. It's probably going to be a bit slower especially if you allocate and free many small blocks, as most compilers include quite powerful optimizations around malloc/free new/delete and the runtime library.   However given your app is probably doing I/O, network operations and so on, these may be the slowest parts of your app, so you may not notice the difference.  [slowest parts of the app are usually the major constraints on performance]

3. If using C++ you can overload new and delete to work with these blocks.  If using MFC have a look at the DEBUG_NEW stuff and this should give you some pointers on how to do this.

I've never measured this.  If you use approach #3 then you can flip between the 2 ways of doing this and compare - that way you can get a timing measurement specific for your app.


0
 
fidodidoAuthor Commented:
Hi,

Thnak you ! Can you give some more information on CoTaskMemAlloc()/CoTaskMemeFree() implenetation/working/behaviour ? I tried to find in MFC help but its not very clear.

Thanks agin
Prashanth
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Answers2000Commented:
To allocate

LPVOID lpvoid = CoTaskMemAlloc( how_many_bytes ) ;

if ( lpvoid == NULL )
{
  // memory allocation failed, you need to add checks for this
}

You can then access memory through a normal pointer.

The initial state of the memory is undefined, use memset if you want to set all initial contents to 0.  You can use memcpy and other similar library functions to poke around in the memory, or simply access the memory using a pointer (casting if necessary)

e.g.
char * pMemoryAsChars = (char *)lpvoid ;
*pMemoryAsChars = 'A' ; /* First char in memory is now an 'A' */


To free (only do this if CoTaskMemAlloc succeeded)

CoTaskMemFree( lpvoid ) ;

Is this the info you need ?
0
 
fidodidoAuthor Commented:
Thank you

Prashanth


0
 
fidodidoAuthor Commented:
Thank you

Prashanth


0
 
fidodidoAuthor Commented:
Hi Answer2000,

While I was rating your answer, accidently I clicked on 'reject'. Actually I wanted to 'accept' it. So please respond again so that I can accept your answer.

Thank you again
Prashanth
0
All Courses

From novice to tech pro — start learning today.