# Allocating memory and releasing the same [closed]

I was going through a code snippet...

try
{
...
// Memory is allocated here...
NmsMessage* pNmsMessage = new NmsMessage(NMS_MODEM_DIAL_NUMBER);
...
{
// pNmsMessage is passed to another thread.
}
return NOERROR;
}
catch(...)
{
...
}


In the PostThreadMessage, memory pointed by pNmsMessage is getting released.

My question:

1. Is this the right way to handle memory in case of threaded environment?
2. If it is not threaded environment, is it better to release the memory before returning from the function and also in the catch block(I don't want to use smart pointers)?

## closed as off-topic by Jerry Coffin, Ben Aaronson, rolfl♦Jun 7 '14 at 18:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions must involve real code that you own or maintain. Questions seeking an explanation of someone else's code are off-topic. Pseudocode, hypothetical code, or stub code should be replaced by a concrete example." – Jerry Coffin, Ben Aaronson, rolfl
If this question can be reworded to fit the rules in the help center, please edit the question.

• Why don't you want to use smart pointers? You may as well be using C at that point. – Martin York Jun 6 '14 at 10:22

I could see passing the pointer to another thread, and the other thread deleting the object. (Gives me the willies, but it at least makes sense.) But if PostThreadMessage itself is doing the deletion, that feels wrong. I'm used to having a delete for every new, and PostThreadMessage is currently breaking that all to hell. Now i have to go rooting around just to figure out who owns an object, so i don't accidentally delete it twice and open up a wormhole or something.

If the pointee doesn't need to outlive the call to PostThreadMessage, then you should be able to put it in a local variable and pass a pointer to the local. Of course, that also means PostThreadMessage needs to keep its paws off the delete button. :P

try {
...
NmsMessage nmsMessage(NMS_MODEM_DIAL_NUMBER);
...
{

nmsMessage, having "automatic storage duration", will be deleted when control leaves the block. Ownership and intent are clearer, you don't need to fart around with new and delete, and C++ cleans up for you automatically whether you return normally or throw an exception.
If PostThreadMessage wants the message info to outlive this code, seems to me that it should be making its own copy. But it kinda sounds like it's already doing that.
(Note, most of this answer assumes that you can change PostThreadMessage. If you can't -- for example, if it's part of some API you're stuck using -- then the only guarantees you have are the ones the API makes. If it doesn't promise that pNmsMessage is either deleted or not in each case that may occur, then you have a pretty serious problem; any dynamically allocated object must be deleted, but deleting it twice can cause major problems.)