memory allocation (malloc vs CoTaskMemAlloc)
Exchange Server Forum Index Exchange Server
Discussion forums for Microsoft Exchange Server users.
Microsoft Outlook
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web ExchangeServerHelp.com
memory allocation (malloc vs CoTaskMemAlloc)

 
Post new topic   Reply to topic    Exchange Server Forum Index -> Development
Author Message
Eric Lajeunesse
Guest





Posted: Wed Dec 21, 2005 11:58 pm    Post subject: memory allocation (malloc vs CoTaskMemAlloc) Reply with quote

Dear Exchange developpers,

I'm running a transport event sink build in C++ for a while on my Exchange
2000 and sometimes error happens that I do not understand why :(. For
example, sometime I'm blocking a message with this source code and for
example sometimes it doesn't work :(.

//------code------
pMailMsg->PutDWORD(IMMPID_MP_MESSAGE_STATUS, MP_STATUS_ABORT_DELIVERY);
pMailMsg->Commit(NULL);
//-----------------


I was thinking that memory allocation migth be the source of the problem. In
my event sink I use the "new" and "malloc" command to allocate memory. I've
seen that a lot of people are using "CoTaskMemAlloc" to allocate memory since
the sink is COM application. Actually, I don't know clearly the difference
beetween those function.

Some of you know if it's safe to use function like "malloc" in my event sink
COM class ? And what about my custom libraries (DLL) loaded in my event sink,
it should use "malloc" or "CoTaskMemAlloc" ?

Any help will be apreciate,

Eric Lajeunesse

Back to top
Dan Mitchell
Guest





Posted: Thu Dec 22, 2005 1:38 am    Post subject: Re: memory allocation (malloc vs CoTaskMemAlloc) Reply with quote

=?Utf-8?B?RXJpYyBMYWpldW5lc3Nl?=
<EricLajeunesse@discussions.microsoft.com> wrote in
news:AFBC3593-AE7C-4721-B7A6-E1DAE877DB24@microsoft.com:
Quote:
Some of you know if it's safe to use function like "malloc" in my
event sink COM class ? And what about my custom libraries (DLL) loaded
in my event sink, it should use "malloc" or "CoTaskMemAlloc" ?

The most important thing is to be consistent.

If you allocate memory with malloc(), you must use free() to release
it.
If you allocate memory with new, you must use delete to release it.
If you allocate memory with new[], you must use delete[] to release it.
If you allocate memory with CoTaskMemAlloc, you must use CoTaskMemFree
to release it.

If you're passing memory out to the outside world and the outside world
will free it, then you need to allocate memory appropriately so that hte
outside world will use the correct method to release it.

COM says that any memory passed as OUT arguments should be allocated by
the called function using CoTaskMemAlloc and freed by the caller using
CoTaskMemFree; that's the rules of COM, though, and it will only apply
if you have code that allocates memory (for a string, structure,
whatever) and passes it out to the outside world. See
http://blogs.msdn.com/larryosterman/archive/2005/07/08/436922.aspx for
more on that.

Your code sample was only using DWORDs, so this shouldn't make a direct
difference there.

-- dan
Back to top
Eric Lajeunesse
Guest





Posted: Thu Dec 22, 2005 5:58 pm    Post subject: Re: memory allocation (malloc vs CoTaskMemAlloc) Reply with quote

Thanks dan for your advice and information. Now this point is clearer for me.
So I don't think memory allocation is my problem since i'm consistent in my
software.

Eric Lajeunesse


"Dan Mitchell" wrote:

Quote:
=?Utf-8?B?RXJpYyBMYWpldW5lc3Nl?=
EricLajeunesse@discussions.microsoft.com> wrote in
news:AFBC3593-AE7C-4721-B7A6-E1DAE877DB24@microsoft.com:
Some of you know if it's safe to use function like "malloc" in my
event sink COM class ? And what about my custom libraries (DLL) loaded
in my event sink, it should use "malloc" or "CoTaskMemAlloc" ?

The most important thing is to be consistent.

If you allocate memory with malloc(), you must use free() to release
it.
If you allocate memory with new, you must use delete to release it.
If you allocate memory with new[], you must use delete[] to release it.
If you allocate memory with CoTaskMemAlloc, you must use CoTaskMemFree
to release it.

If you're passing memory out to the outside world and the outside world
will free it, then you need to allocate memory appropriately so that hte
outside world will use the correct method to release it.

COM says that any memory passed as OUT arguments should be allocated by
the called function using CoTaskMemAlloc and freed by the caller using
CoTaskMemFree; that's the rules of COM, though, and it will only apply
if you have code that allocates memory (for a string, structure,
whatever) and passes it out to the outside world. See
http://blogs.msdn.com/larryosterman/archive/2005/07/08/436922.aspx for
more on that.

Your code sample was only using DWORDs, so this shouldn't make a direct
difference there.

-- dan



Back to top
 
Post new topic   Reply to topic    Exchange Server Forum Index -> Development All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server Dedicated Servers
New Topics Powered by phpBB