Thread: double free or corruption (fasttop) help

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    41

    double free or corruption (fasttop) help

    I am sure the operator= isn't overloaded correctly but how can I fix this? My friend looked at this and also helped me on the overload operators and we have identical operators to help debug. Did I type something wrong?

    Code:
    *** glibc detected *** queue: double free or corruption (fasttop): 0x0907e008 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x48c268c9]
    queue[0x804922b]
    queue[0x8048ef5]
    queue[0x8048c51]
    queue[0x80489cc]
    queue[0x8049714]
    /lib/libc.so.6(__libc_start_main+0xf5)[0x48bca635]
    queue[0x8048881]
    ======= Memory map: ========
    08048000-0804b000 r-xp 00000000 09:03 3663402    
    0804b000-0804c000 rw-p 00002000 09:03 3663402    
    0907e000-0909f000 rw-p 00000000 00:00 0          [heap]
    48b8e000-48bad000 r-xp 00000000 68:05 483370     /usr/lib/ld-2.15.so
    48bad000-48bae000 r--p 0001e000 68:05 483370     /usr/lib/ld-2.15.so
    48bae000-48baf000 rw-p 0001f000 68:05 483370     /usr/lib/ld-2.15.so
    48bb1000-48d5c000 r-xp 00000000 68:05 483373     /usr/lib/libc-2.15.so
    48d5c000-48d5d000 ---p 001ab000 68:05 483373     /usr/lib/libc-2.15.so
    48d5d000-48d5f000 r--p 001ab000 68:05 483373     /usr/lib/libc-2.15.so
    48d5f000-48d60000 rw-p 001ad000 68:05 483373     /usr/lib/libc-2.15.so
    48d60000-48d63000 rw-p 00000000 00:00 0
    48d81000-48daa000 r-xp 00000000 68:05 483865     /usr/lib/libm-2.15.so
    48daa000-48dab000 r--p 00029000 68:05 483865     /usr/lib/libm-2.15.so
    48dab000-48dac000 rw-p 0002a000 68:05 483865     /usr/lib/libm-2.15.so
    48f41000-48f5d000 r-xp 00000000 68:05 483949     /usr/lib/libgcc_s-4.7.2-20120921.so.1
    48f5d000-48f5e000 rw-p 0001b000 68:05 483949     /usr/lib/libgcc_s-4.7.2-20120921.so.1
    4930f000-493ef000 r-xp 00000000 68:05 484683     /usr/lib/libstdc++.so.6.0.17
    493ef000-493f3000 r--p 000e0000 68:05 484683     /usr/lib/libstdc++.so.6.0.17
    493f3000-493f4000 rw-p 000e4000 68:05 484683     /usr/lib/libstdc++.so.6.0.17
    493f4000-493fb000 rw-p 00000000 00:00 0
    b77c3000-b77c6000 rw-p 00000000 00:00 0
    b77dc000-b77e0000 rw-p 00000000 00:00 0
    b77e0000-b77e1000 r-xp 00000000 00:00 0          [vdso]
    bfccf000-bfcf0000 rw-p 00000000 00:00 0          [stack]
    Abort
    Code:
    Queue :: Queue(int size)
    {       size=0;
            count=0;
            MaxElements=256;        }
    
    
    Queue :: ~Queue()
    {       clear();                }
    
    
    Queue :: Queue(const Queue& other)
    {       InitFromCopy(other);    }
    
    
    Queue Queue :: operator= (const Queue& other)
    {
            //clear();
            InitFromCopy(other);
            return *this;
    }
    
    
    bool Queue :: operator== (const Queue& other) const
    {
            for(int i=0; i<count; i++)
            {
                    if(qa.get(i) != other.qa.get(i))
                    {       return false;           }
            }
            return true;
    }
    
    
    bool Queue :: operator!= (const Queue& other) const
    {
            if((*this)==other)
                    return false;
            return true;
    }
    
    
    ostream& operator << (ostream& s, const Queue& q)
    {
            s << q.front() << "\t<-- front" << endl;
    
    
            for(int i=1; i<q.size(); i++)
                    s << q.qa.get(i) << endl;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    No idea, but if you compile your code with the -g flag, like

    g++ -g prog.cpp

    Then
    queue[0x804922b]
    queue[0x8048ef5]
    queue[0x8048c51]
    queue[0x80489cc]
    queue[0x8049714]

    Will be replaced with actual function names within your code, so you can more easily trace what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There isn't enough to be able to tell where the problem is necessaarily from looking at that code, but your operator == should be comparing count and other.count before it decides to loop.
    Show InitFromCopy
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Aren't you in the wrong forum?

    Bye, Andreas

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good observation, so not anymore.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    Sorry guys was busy. Will post up initfromcopy code up. It should be there tho

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Post the code that's calling this as well - and InitFromCopy() and clear()

    Also, FYI, it's bad (and can lead to incorrect behavior with many templated algorithms) that operator== and operator!= can both return false. Your implementation should make them boolean inverses unless there's a really good need to do otherwise.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typically, it would be best if you could post the smallest possible compilable example that demonstrates the problem. Minimize it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    Code:
    Queue :: Queue(int size)
    {	size=0;
    	count=0;
    	MaxElements=256;	}
    
    
    Queue :: ~Queue()
    {	clear();		}
    
    
    Queue :: Queue(const Queue& other)
    {	InitFromCopy(other);	}
    
    
    Queue Queue :: operator= (const Queue& other)
    {
    	//clear();
    	InitFromCopy(other);
    	return *this;
    }
    
    
    bool Queue :: operator== (const Queue& other) const
    {
    	for(int i=0; i<count; i++)
    	{
    		if(qa.get(i) != other.qa.get(i))
    		{	return false;		}
    	}	
    	return true;
    }
    
    
    bool Queue :: operator!= (const Queue& other) const
    {
    	if((*this)==other)
    		return false;
    	return true;
    }
    
    
    ostream& operator << (ostream& s, const Queue& q)
    {
    	s << q.front() << "\t<-- front" << endl;
    
    
    	for(int i=1; i<q.size(); i++)
    		s << q.qa.get(i) << endl;
    
    
    	return s;
    
    
    }
    
    
    // Accessors
    int Queue :: size() const
    {	return count;	}
    
    
    bool Queue :: isempty() const
    {
    	if(count)
    		return false;
    	return true;
    }
    
    
    bool Queue :: isfull() const
    {
    	if(count==MaxElements)
    		return true;
    	return false;
    }
    
    
    Entry Queue :: front() const
    {	return qa.get(0);	}
    
    
    
    
    // Mutators
    void Queue :: clear()
    {
    	qa.clear();
    	count = 0;
    }
    
    
    void Queue :: push(Entry x)
    {	
    	if(isfull())
    		cout << "Queue is full!" << endl;
    	else
    	{
    		qa.insert(x);
    		count++;
    	}
    }
    
    
    Entry Queue :: pop(void)
    {	
    	if(isempty())
    		cout << "The Queue is empty!" << endl;
    	else
    	{
    		count--;
    		return qa.remove(0);
    	}
    }
    
    
    Entry Queue :: front(void)
    {
    	return qa.get(0);
    }
    
    
    // Private
    void Queue :: InitFromCopy(const Queue& other)
    {
    //	qa = other.qa;
    //	MaxElements = other.MaxElements;
    //	count = other.count;
    
    
    	qa = other.qa;
    	MaxElements = other.MaxElements;
    
    
    	for(int i=0; i<size(); i++)
    		qa.insert(other.qa.get(i));
    }
    You will notice there are functions called "Entry" there is no code for those so they are working fine. I have posted everything that should relate to the error. I will be at relay for life today so I won't get on the computer much for the rest of the day but will try to check back later tonight.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the definition of Queue, without which we would not know how the member variables are declared, which is probably critical to understanding if what you are doing is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The definition of "Entry" might also be relevant here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    The definition of "Entry" might also be relevant here.
    I agree.

    I've lost count of the number of times in these forums that a poster has omitted code that they insist is "working fine", only for that code to emerge later as the culprit.

    Code might have passed a few tests, but that doesn't mean those tests are sufficient.

    Bugs are much harder to find when they are in code that is not provided, is not being examined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    So in Entry:
    Code:
    typedef char Entry;
    Definition of Queue:
    Code:
    const int MaxQueueSize = 100;
    
    
    
    
    class Queue
    {   public:
    
    
    	Container qa;
    	
    	Queue(int size = MaxQueueSize);
    	Queue(const Queue& other);
    	~Queue();
    
    
    	Queue operator= (const Queue& other);
    	bool operator== (const Queue& other) const;
    	bool operator!= (const Queue& other) const;
    	friend ostream& operator<< (ostream& s, const Queue& qa);
    
    
    	int size() const;
    	bool isempty() const;
    	bool isfull() const;
    	Entry front() const;
    	
    
    
    	void clear();
    	void push(Entry x);
    	Entry pop(void);
    	Entry front(void);
    
    
    
    
       private:
    	int MaxElements;
    	Container a;
    	int count;
    	void InitFromCopy(const Queue& other);
    };

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the definition of Container, and why do you have a public member variable of that type in addition to the private member variable of that type?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    Quote Originally Posted by Salem View Post
    No idea, but if you compile your code with the -g flag, like

    g++ -g prog.cpp

    Then
    queue[0x804922b]
    queue[0x8048ef5]
    queue[0x8048c51]
    queue[0x80489cc]
    queue[0x8049714]

    Will be replaced with actual function names within your code, so you can more easily trace what is going on.
    Can you show me how to put it in a makefile? I have been trying but I have no luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double free or corruption (fasttop)
    By pepito in forum C Programming
    Replies: 6
    Last Post: 01-20-2011, 11:18 AM
  2. double free or corruption (out)
    By Mouser58907 in forum C Programming
    Replies: 5
    Last Post: 02-25-2009, 12:20 AM
  3. double free or corruption (fasttop)
    By yougene in forum C Programming
    Replies: 6
    Last Post: 01-17-2009, 06:44 PM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  5. double free or corruption???
    By hwttdz in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2006, 03:02 PM