Solved

Memeory allocation in COM Components and Clinet programs

Posted on 1998-09-30
7
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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