LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-08-2009, 02:54 PM   #1
ETCKerry
Member
 
Registered: Sep 2008
Posts: 37

Rep: Reputation: 15
Could Valgrind be wrong?


Hello,

I'm trying to track down a memory leak using Valgrind, and was surprised by the number of "definitely lost" messages it threw at me, especially in one case where I have the following:

- A template class that manages a list of pointers (MY_LIST)
- A data object (DATA)
- A class that contains a MY_LIST and many pointers to DATA objects (CREATOR)

Some code snippets:

From my_list_class.h (some functions omitted):
Code:
template <class T>
class MY_LIST
{
public:
	// Constructor
	MY_LIST();

	// Destructor
	~MY_LIST();

	// Private data accessors
	int Add(T *ToAdd);
	void Remove(const int &Index);
	inline int GetCount(void) const { return Count; };

	// Removes all objects from the list
	void Clear(void);

	// Operators
	T *operator [](const int &Index) const;

private:
	// The number of objects in this list
	int Count;

	// The data in the list
	T **ObjectList;
};

template <class T>
MY_LIST<T>::MY_LIST()
{
	// Initialize the count to zero and the pointers to NULL
	Count = 0;
	ObjectList = NULL;
}

template <class T>
MY_LIST<T>::~MY_LIST()
{
	// Remove all of the items in this list
	Clear();
}

template <class T>
void MY_LIST<T>::Clear(void)
{
	// Delete all of the items in the list
	int i;
	for (i = 0; i < Count; i++)
	{
		delete ObjectList[i];
		ObjectList[i] = NULL;
	}

	// Delete the list and set the count to zero
	delete [] ObjectList;
	ObjectList = NULL;
	Count = 0;

	return;
}
So when a MY_LIST is deleted, it deletes all of the objects that it's list of pointers point to, as well as the list itself.

The DATA object's constructor takes a pointer to a list, and it adds itself to the list in the constructor. So DATA:ATA() looks something like this:
Code:
DATA::DATA(MY_LIST<DATA>* List)
{
	List->Add(this);
}
And then I create a bunch of DATA objects in the CREATOR class, but I don't explicitly delete them. Instead, I call MY_LIST::Clear. This class looks like this:
Code:
// This class contains a private MY_LIST<DATA> object called List
CREATOR::CREATOR()
{
	// Make a bunch of DATA objects
	DATA* Data1 = new DATA(this);
	DATA* Data2 = new DATA(this);
	DATA* Data3 = new DATA(this);
	// and a whole lot more...
}

CREATOR::~CREATOR()
{
	List.Clear();
}
When I run Valgrind, it tells me every one of my DATA objects has leaked. I don't believe that this is correct, but I read that when Valgrind says "definitely lost" it is 100% sure.

I had CREATOR::CREATOR() print the addresses of the new objects to file, and I had MY_LIST::Clear() print the addresses of the object just before deletion to file, and the lists are identical... so this is a case where Valgrind is wrong?

Any ideas? I can post more code or a complete example if it will help.

Thanks,

Kerry
 
Old 08-10-2009, 07:27 AM   #2
jeromeNP7
Member
 
Registered: Jun 2009
Posts: 101

Rep: Reputation: 19
I would vote for Valgrind. C++ compilation is a complex task and a lot has to be done beyond what is implied by your own code. As a test you could compile your code with another C++ compiler and see if Valgrind throws different results.

Linux

Last edited by jeromeNP7; 09-04-2009 at 09:29 PM.
 
Old 08-10-2009, 07:45 AM   #3
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
I'd also would give the point to valgrind, but who knows. I just noticed something in

my_list_class.h within the clear Function
Code:
for (i = 0; i < Count; i++)
	{
		delete ObjectList[i];
		ObjectList[i] = NULL;
	}
You just kill the ObjectList[i] and afterwards assign a varibale to it... Maybe that does not get interpreted very well.
Nother thing I read up is that if you just pass on a pointer value to a another pointer it can lead to memory leakage.
I just found to links that might be of some help.

http://www.yolinux.com/TUTORIALS/C++...moryLeaks.html
This one just talks about memory stuff within c and c++.

http://www.yolinux.com/TUTORIALS/C++...moryLeaks.html
This one just names other programms that look for memory leakage like valgrind

Regards Zhjim
 
Old 08-10-2009, 08:28 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by zhjim View Post
You just kill the ObjectList[i] and afterwards assign a varibale to it...
No. He deleted an object using a pointer, then he assigned NULL to the pointer (not to the object). That is perfectly valid. It is often the only correct simple design in situations very similar to that one. In this case, it was unnecessary, but it still is perfectly OK.

Quote:
Nother thing I read up is that if you just pass on a pointer value to a another pointer it can lead to memory leakage.
Discussion of general programming patterns that could be error prone is not constructive at this level. The author of the code is obviously not a beginner. The avoidance of memory leaks in the posted code seems to be carefully thought out.

I don't see the memory leak. I'm pretty good at spotting them, so while I can't guarantee I didn't miss something, I don't think any memory leak is indicated by the posted code. I have no estimate of whether that means Valgrind is wrong or it means the memory leak is a consequence of some code that wasn't posted.
 
Old 08-11-2009, 04:36 AM   #5
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by johnsfine
Discussion of general programming patterns that could be error prone is not constructive at this level. The author of the code is obviously not a beginner. The avoidance of memory leaks in the posted code seems to be carefully thought out.
Hard to guess the level a programmer has but your definetly right. I shut my gab and let the big ones take over
 
Old 08-14-2009, 11:37 AM   #6
ETCKerry
Member
 
Registered: Sep 2008
Posts: 37

Original Poster
Rep: Reputation: 15
The point goes to Valgrind. It took me quite a bit of time to track it down, but I oversimplified in my original post and it turns out there really was a leak, but it was a result of code that wasn't posted. Just thought I'd check back to even the score...

Thanks for all the replies.

-Kerry
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
VALGRIND-Query inramana Programming 3 04-13-2010 12:41 AM
Valgrind - an answer. cyent Programming 6 02-11-2010 01:49 PM
Cannot use valgrind g4j31a5 Programming 1 01-12-2007 04:24 AM
valgrind with C++ yashwantpinge Programming 2 02-16-2006 03:56 PM
Valgrind not included? Zeno McDohl Slackware 1 02-05-2006 03:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration