Solved

Memory leak

Posted on 2004-09-16
13
420 Views
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.
TK


0
Comment
Question by:albusaidi
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 11

Expert Comment

by:avizit
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 ?
0
 
LVL 11

Expert Comment

by:avizit
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

0
 

Author Comment

by:albusaidi
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.
0
 
LVL 11

Expert Comment

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

0
 

Author Comment

by:albusaidi
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.
0
 
LVL 23

Accepted Solution

by:
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:pankajtiwary
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.
0
 

Author Comment

by:albusaidi
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
0
 
LVL 16

Expert Comment

by:PaulCaswell
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.

Paul
0
 
LVL 22

Expert Comment

by:grg99
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.


0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12083597
Greg,

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.

Paul
0
 

Author Comment

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

Author Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

747 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

9 Experts available now in Live!

Get 1:1 Help Now