Solved

Memeory allocation in COM Components and Clinet programs

Posted on 1998-09-30
7
291 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:fidodido
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322732
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
 

Author Comment

by:fidodido
ID: 1322733
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322734
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:fidodido
ID: 1322735
Thank you

Prashanth


0
 

Author Comment

by:fidodido
ID: 1322736
Thank you

Prashanth


0
 

Author Comment

by:fidodido
ID: 1322737
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
 
LVL 8

Accepted Solution

by:
Answers2000 earned 70 total points
ID: 1322738
Responding again Prashanth

See you round
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 105
EvenOdd challenge 10 128
FizzBuzz challenge 9 82
firstChar challenge 13 121
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question