Memory leak

Posted on 2004-09-16
Last Modified: 2010-04-01
HI .. Im using a tool to find any possible for memory leak ....
let is see this code for example:

 // some src
 *msg = new (something)
  if (msg->send== false) // message not send
   delete msg;
// some src

the tool point here as possible place for memory leaking
beacuse  if stetment not exe , the msg will not deleted  
but if I write in the last line delete msg , that will be ok
but the msg have been sent to some where else that means It will effect the msg
the Client force me to use this tool
so any one have an idea how can I change in the code so I will not get warring from the tool (client wants the report from this tool empty of warring)

what I want to say that my src code is right but the tool catch it as aplce for possible leak ... I must use the tool this why I want to get an idea od changing the src.

Question by:albusaidi
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
  • 5
  • 3
  • 2
  • +3
LVL 11

Expert Comment

ID: 12081212
is it because you are deleting msg only if the condition is satisfied

so if its not satisfied , you dont delete and hence is a memory leak ?
LVL 11

Expert Comment

ID: 12081220
you should delete msg after you no longer need it.

so whenyou are done dealing with the msg then you can delete , instead of deleting under a if condition


Author Comment

ID: 12081275
but the problem I don't know when I will not use it any more because I sent but I don't know when the other side will finish use it.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 12081324
In that case you should delete only after the other side has finished using it or just before program termination


Author Comment

ID: 12081345
the program will not terminate at all, is running for ever .... I will try to see how can I know that the other side already finish used it.
LVL 23

Accepted Solution

brettmjohnson earned 125 total points
ID: 12081460
Your problem is not very clearly stated.  However I will attempt to interpret it as follows:

The above code creates an instance of an object.
That object is handed off (sent) to another [asynchronous] chunk of code to use.
You don't want to delete the object before all other [asynchronous] users of the object no longer need it.
You do want to delete the object if no other users of the object require it.

I have done this before in several different OO environments (but not C++).
I have called these "Autonomous Objects", for they stand alone and are not really "owned" by anyone,
although they may be referred to by other objects.  Garbage collecting languages such as Java handle
this type of object quite well, but C++ is not a GC language.  

I have managed these types of objects by use-counting them.  As long as some object/task/thread
has interest in the object, it attaches/subscribes to the object.  The attach() method bumps an
internal count.  When that object/task/thread no longer has interest in the object, it detaches/unsubscribes
from the object (rather than deleting it).  The detach() method decrements the internal count.  
When the count goes to zero, the object commits suicide, calling its own destructor (or 'delete this;').
In languages where you can override the destructor, you can manage the count in the destructor
(decrementing the count and simply returning if it is > 0), but the semantics of attaching/detaching
get clouded.

Expert Comment

ID: 12081609
Hello all,

What I could not understand is the poster has allocated memory via new and assigns it to *msg, at the time of deallocating the memory the poster is freeing msg. I don't know whether he has done it intentionally. From the code it looks that msg is a double pointer. But then when the address of the allocated memory is placed in *msg, to free that very memory you need to pass delete the same address which should be *msg. Ofcourse the whole point exists if between new and delete you have not made any changes to *new (or assigned *new to new).

Also, if the condition doesn't satisfy, you are deallocating it. You need to correct this too.

Author Comment

ID: 12082192
Yes  brettmjohnson this what I mean .. thank You I'm not that good in English.
can You please give a few lines as example to understand Your way or any metiral in the internet to understand it ... TK
LVL 16

Expert Comment

ID: 12082509
It seems that the problem here is not in the code sample but in the code checking tool.

There are many situations where an autonomous object is required, as Brett quite rightly points out. If this checking tool is as good as the clients think it is then it should be possible to tell the checker that this is an object of this type.

I would suggest you do one or more of:

1. Study the documentation for the checker to see if there is a way of marking the object in this way.
2. Tell us which checker it is in case one of us knows it well.
3. Make the 'Send' method of the object send a serialized version of the object, not the object itself.

LVL 22

Expert Comment

ID: 12083257
Wait a bleepin' minute, everyone!

You all seem to be missing a very basic point.  There seem to be at least THREE distinct cases  possible:

(1) If "send" makes a copy of the message object, or the message data (VERY VERY likely if the target is in another process, address space, or computer), then we CAN delete the message object after a send, whether it succeeds or not.

(2)  If "send" doesnt make a copy, but passes an address or reference of the object, BUT then the receiving method makes a copy owhen it receives the data, but before it acknowledges correct reception, then we can delete the object.

(3)  If "send" doesnt make a copy, but passes an address or reference of the object, AND then the receiving method DOESNT make a copy, but uses the original object or data  then obviously we can't delete the message object or data until the message has been processed by the receiver

Since we don't know the semantics of this "send" method, so we can have NO OPINION what to do with the object.

My suggestion to the original poster, carefully read the documentation for send() and see if it returns before the data has been processed or if it makes a copy of the data.  You might have to also lok at the receiving method documentation to see if IT makes a copy of the data when it is received.

LVL 16

Expert Comment

ID: 12083597

Absolutely! And I am guessing that this code checker, if it is as all-singing-all-dancing as this guy's customer thinks it is then there 'SHOULD' be a way of telling it that this object is now in your state '3' or what Brett called "Autonomous Object" and need not be kept in its 'possible leak' list. If, however, there isnt a technique for doing this then we must consider alternatives such as serializing objects across the barrier.

This looks likely to be a slight variant of your scenario 3 because it is possible that it is the 'receiver' that deletes the object. Remember that the receiver may be in a library and therefore unavailable for tinkering, not another process on the same machine. Perhaps the library serialises the object, sends it over some channel and then deletes it.


Author Comment

ID: 12091829
Thanks allot for every body ... I want to add that I trust the tool because I think it is the best  ( and really I don't know that mush about the source code because it is almost 1 million lines .

Author Comment

ID: 12099558
Mr. brettmjohnson  can I get a clear example about your way to solve the problem....
or any webpage talking about that

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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