Solved

Memeory allocation in COM Components and Clinet programs

Posted on 1998-09-30
7
287 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

805 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