Solved

Memeory allocation in COM Components and Clinet programs

Posted on 1998-09-30
7
282 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 49
Doc'in system (example?) BA 7 78
bigDiff challenge 17 75
MaxSpan challenge 9 65
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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now