| | |
Crazy Leak...plz help
Thread Solved
![]() |
this is the final version of the priority queue i implemented {with some of your help!}...
the problem is that when i profile the code with valgrind, it shows that i have a memory leak....
i can also see that, if in my code i make certain test_points{where i pause the execution} and then check out the process manager....It seems that although i clean the memory, something stays behind...
i've used simple segregated storage scheme that vijayan121 suggested.. i also have a version where i visit every node to deleting it... using a preorder tree walk, or the method Hamrick suggested {with a stack}
all versions, have the exact same problem, that tells me that memory leak is not in the structure .....but where else can it be??
I attach the version with the segregated scheme along with the makefile i used to test the structure with valgrind
thank you in advance for your time...
the problem is that when i profile the code with valgrind, it shows that i have a memory leak....
i can also see that, if in my code i make certain test_points{where i pause the execution} and then check out the process manager....It seems that although i clean the memory, something stays behind...
i've used simple segregated storage scheme that vijayan121 suggested.. i also have a version where i visit every node to deleting it... using a preorder tree walk, or the method Hamrick suggested {with a stack}
all versions, have the exact same problem, that tells me that memory leak is not in the structure .....but where else can it be??
I attach the version with the segregated scheme along with the makefile i used to test the structure with valgrind
thank you in advance for your time...
If this is for a school assignment then you probably should not use the boost library without your instructor's prior permission because he (or she) may not be able to compile it on school computers.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
check the function
the 2 reachable blocks are not memory allocated from your code (they are allocations by libc.so and will be released on program exit) and do not constitute a leak.
LeftistHeap::initializeHeap. valgrind is reporting 80 bytes in 10 unreachable blocks of memory allocated in this function. this is a leak.the 2 reachable blocks are not memory allocated from your code (they are allocations by libc.so and will be released on program exit) and do not constitute a leak.
•
•
•
•
If this is for a school assignment then you probably should not use the boost library without your instructor's prior permission because he (or she) may not be able to compile it on school computers.
•
•
•
•
check the functionLeftistHeap::initializeHeap. valgrind is reporting 80 bytes in 10 unreachable blocks of memory allocated in this function. this is a leak.
the 2 reachable blocks are not memory allocated from your code (they are allocations by libc.so and will be released on program exit) and do not constitute a leak.
LeftistHeap::initializeHeap does:In orderd to get a linear time initialization i create n heaps with each containing one of the n elements. Then i place these heaps on a FIFO queue. Then the heaps are deleted from this queue in pairs, merged, and added to the end of the queue until only one heap remains.This heap assign to root pointer....
c++ Syntax (Toggle Plain Text)
void LeftistHeap::initializeHeap(std::vector<int> & vec) { queue<LeftistHeap *> q; for(int i=0; i<vec.size(); ++i) { q.push(new LeftistHeap(vec[i]) ); } LeftistHeap *temp; while ( q.size()!=1) { temp = q.front(); q.pop(); //the line below equals to :: temp = merge(temp, q.front()) temp->merge( *(q.front()) ); q.pop(); q.push(temp); } // assign final heap..... root=merge(q.front()->root, NULL); }
here is the only line where i allocate space in the program
C++ Syntax (Toggle Plain Text)
q.push(new LeftistHeap(vec[i]) );
So to see if this has any problems i created a new ain that does only this::
C++ Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { LeftistHeap init(5); return 0; }
if i test this programm with valgrind, i don't get any errors.... which is weird because i shoud inlcude this line to have it empty the pool{if i have understand correctly the operation of the pool}
C++ Syntax (Toggle Plain Text)
myNode::node_pool.purge_memory() ;
any other ideas?
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
>> here is the only line where i allocate space in the program
the leak is because of that line; sizeof(LeftistHeap) == sizeof(pointer) + sizeof(int), adds up to 8 bytes on i386. the leak reported by valgrind is
and in main.cpp we have
the blocks lost are the 10 LeftistHeap objects that you allocate using new and never delete.
change HOW_MANY_ELEMENTS to 1000 and valgrind will report 8000 bytes in 1000 blocks as definitely lost.
the dynamically allocated LeftistHeap objects are leaking; not the nodes
>> if i test this programm with valgrind, i don't get any errors.... which is weird...
valgrind automagically knows about memory that is still reachable. node_pool (has a static storage duration; still not destroyed at the point main returns) has references the the blocks it has allocated; so the memory is not *definitely* lost. in fact, the destructor of node_pool will free this memory.
you could verify this by using the valgrind switch --show-below-main=yes (continue stack traces below main)
q.push(new LeftistHeap(vec[i]) );the leak is because of that line; sizeof(LeftistHeap) == sizeof(pointer) + sizeof(int), adds up to 8 bytes on i386. the leak reported by valgrind is
•
•
•
•
80 bytes in 10 blocks are definitely lost in loss record 1 of 2
==1599== at 0x3C03D2F3: operator new(unsigned) (in /usr/local/lib/valgrind/vgpreload_memcheck.so)
==1599== by 0x804936E: LeftistHeap::initializeHeap(std::vector<int, std::allocator<int> >&) (LeftistHeap.cpp:206)
enum { HOW_MANY_ELEMENTS = 10, TEST_ITERATIONS = 1, DISTRIBUITION_AREA=1024*1024 };the blocks lost are the 10 LeftistHeap objects that you allocate using new and never delete.
change HOW_MANY_ELEMENTS to 1000 and valgrind will report 8000 bytes in 1000 blocks as definitely lost.
the dynamically allocated LeftistHeap objects are leaking; not the nodes
>> if i test this programm with valgrind, i don't get any errors.... which is weird...
valgrind automagically knows about memory that is still reachable. node_pool (has a static storage duration; still not destroyed at the point main returns) has references the the blocks it has allocated; so the memory is not *definitely* lost. in fact, the destructor of node_pool will free this memory.
you could verify this by using the valgrind switch --show-below-main=yes (continue stack traces below main)
Last edited by vijayan121; Sep 7th, 2007 at 10:29 am.
•
•
•
•
q.push(new LeftistHeap(vec[i]) );
the leak is because of that line;
queue<LeftistHeap *> q; would be made responsible for desposing the elements....•
•
•
•
sizeof(LeftistHeap) == sizeof(pointer) + sizeof(int), adds up to 8 bytes on i386. the leak reported by valgrind is
and in main.cpp we have
enum { HOW_MANY_ELEMENTS = 10, TEST_ITERATIONS = 1, DISTRIBUITION_AREA=1024*1024 };
the blocks lost are the 10 LeftistHeap objects that you allocate using new and never delete.
•
•
•
•
change HOW_MANY_ELEMENTS to 1000 and valgrind will report 8000 bytes in 1000 blocks as definitely lost.
the dynamically allocated LeftistHeap objects are leaking; not the nodes
•
•
•
•
valgrind automagically knows about memory that is still reachable. node_pool (has a static storage duration; still not destroyed at the point main returns) has references the the blocks it has allocated; so the memory is not *definitely* lost. in fact, the destructor of node_pool will free this memory.
you could verify this by using the valgrind switch --show-below-main=yes (continue stack traces below main)
here is what i did... if the queue isn't responsible then i am, so i did something like this:
c++ Syntax (Toggle Plain Text)
LeftistHeap *a=new LeftistHeap(vec[i]); q.push(a); delete a;
The program compiles but the structure doesn't work afterwards...as i would expect,because i delete where the queue elements point and logically valgrind reports
Invalid read of size 4... So, the question remains , how can i deallocate something that i a not in control, because when i push the pointer in the queue i lose all control on that memory....
thanks again for your time,
nick
PS:
orry, if i ask silly things, but i am kinda lost... •
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
>> but why? i thaught that with this line the queue queue<LeftistHeap *> q; would be made responsible for desposing the elements....
the queue owns pointers to LeftistHeaps, not the LeftistHeaps themselves. so it would release what it owns, ie. the pointer, not the pointed object. the behaviour would have been different for a queue<LeftistHeap>.
>> why the size of leftistheap is just sizeof(pointer) + sizeof(int)??I also have three pointers in the leftistheap class...
the size of an object would include the size of its non-static member variables ( + a vptr if there are virtual functions + one vbaseptr for each virtual base class + any padding; however for yourLeftistHeap none of these apply). the only non-static member variables of a LeftistHeap are
>> here is what i did... if the queue isn't responsible then i am, so i did something like this:
>> .....
>> doesn't work afterwards...as i would expect...
>> how can i deallocate something that i a not in control, because when i push the pointer in the queue i lose all control on that memory....
the queue will safely keep (a copy of) anything that you push into it. and give it back to you on demand.
do something like this instead.
note 1: i do not think that prayer is an effective antidote to problems in software.
note 2: no one is going to accuse you of writing fiendishly efficient code!
the queue owns pointers to LeftistHeaps, not the LeftistHeaps themselves. so it would release what it owns, ie. the pointer, not the pointed object. the behaviour would have been different for a queue<LeftistHeap>.
>> why the size of leftistheap is just sizeof(pointer) + sizeof(int)??I also have three pointers in the leftistheap class...
the size of an object would include the size of its non-static member variables ( + a vptr if there are virtual functions + one vbaseptr for each virtual base class + any padding; however for yourLeftistHeap none of these apply). the only non-static member variables of a LeftistHeap are
myNode *root; int emptyFlag;>> here is what i did... if the queue isn't responsible then i am, so i did something like this:
>> .....
>> doesn't work afterwards...as i would expect...
>> how can i deallocate something that i a not in control, because when i push the pointer in the queue i lose all control on that memory....
the queue will safely keep (a copy of) anything that you push into it. and give it back to you on demand.
do something like this instead.
C++ Syntax (Toggle Plain Text)
void LeftistHeap::initializeHeap(std::vector<int> & vec) { //prey to work!!! queue<LeftistHeap *> q; for(int i=0; i<vec.size(); ++i) { q.push(new LeftistHeap(vec[i]) ); } LeftistHeap *temp; while ( q.size()!=1) { temp = q.front(); q.pop(); //dld temp = merge(temp, q.front()) temp->merge( *(q.front()) ); // delete the LeftistHeap which we have just merged in to temp delete q.front() ; // *** this was added to the original code *** q.pop(); q.push(temp); } //kanoume assign to teliko heap..... root=merge(q.front()->root, NULL); // delete the one temporary LeftistHeap remaning delete q.front() ; // *** this was added to the original code *** }
note 2: no one is going to accuse you of writing fiendishly efficient code!
Thank you for solving this problem!!
that makes sense!
, that couldn't be more true... but when you are frustated with a program, your comments include funny stuff
sorry, my english don't help me...fiendishly efficient code?
that means that the code is efficient or not?
if there are more corrections that can be made, it would be great if you could point them out vijayan!
•
•
•
•
the queue owns pointers to LeftistHeaps, not the LeftistHeaps themselves. so it would release what it owns, ie. the pointer, not the pointed object. the behaviour would have been different for a queue<LeftistHeap>.
•
•
•
•
note 1: i do not think that prayer is an effective antidote to problems in software.
, that couldn't be more true... but when you are frustated with a program, your comments include funny stuff•
•
•
•
note 2: no one is going to accuse you of writing fiendishly efficient code!
that means that the code is efficient or not?if there are more corrections that can be made, it would be great if you could point them out vijayan!
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
clearly the code (creating, merging, destroying a lot of heaps) you have is not very efficient. an alternative would be
1. write a function to add one element to an existing heap (push_heap).
2. to create a heap of N elements, start with an empty heap and iteratively call push_heap for each of the N elements.
this will do away with the need for creation/destruction of N heap objects.
i do not know the leftist heap data structure, but like in the standard heap (make_heap), there could be a faster algorithm to do this in linear time.
1. write a function to add one element to an existing heap (push_heap).
2. to create a heap of N elements, start with an empty heap and iteratively call push_heap for each of the N elements.
this will do away with the need for creation/destruction of N heap objects.
i do not know the leftist heap data structure, but like in the standard heap (make_heap), there could be a faster algorithm to do this in linear time.
•
•
•
•
clearly the code (creating, merging, destroying a lot of heaps) you have is not very efficient. an alternative would be
1. write a function to add one element to an existing heap (push_heap).
2. to create a heap of N elements, start with an empty heap and iteratively call push_heap for each of the N elements.
this will do away with the need for creation/destruction of N heap objects.
i do not know the leftist heap data structure, but like in the standard heap (make_heap), there could be a faster algorithm to do this in linear time.
•
•
•
•
Initializing a height biased leftist tree is primarily done in one of two ways. The first is to merge each node one at a time into one HBLT. This process is inefficient and takes O(nlogn) time. The other approach is to use a queue to store each node and resulting tree. The first two items in the queue are removed, merged, and placed back into the queue. This can initialize a HBLT in O(n) time. This approach is detailed in the three diagrams supplied. A min height biased leftist tree is shown.
![]() |
Similar Threads
- can someone plz help me with this? (Visual Basic 4 / 5 / 6)
- I NEED SUPPORT **"IMPORTANT"** PLZ HELP (Windows NT / 2000 / XP)
- Plz Help Me Here!! OUTLOOK !! (Windows Software)
- Windows media player (Windows NT / 2000 / XP)
- Win 98; Going Crazy; Plz Help Me (Windows 95 / 98 / Me)
- can sum1 look @ dis plz (Viruses, Spyware and other Nasties)
- Hijackthis log file - plz help (Viruses, Spyware and other Nasties)
- IE not working...PLZ help :cry: (Web Browsers)
- PLZ help it's urgent! (Web Browsers)
- plz help ppl...... (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Time Complexity
- Next Thread: Core dump segment fault
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






