LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2007, 05:08 PM   #1
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Rep: Reputation: 15
*** glibc detected *** free(): invalid pointer: 0x0804b094 ***


I get this error when running a function that calls delete on pointers. Everything compiles fine. I checked the addresses of the pointers and they're all different. This is in the dtor:
Code:
void Queue::~Queue()
{
	for(unsigned int i = 0; i < count; i++)
	{
		delete &array[head];
		head++;
		if(head == array.Length())
		{
			head = 0;
		}
	}
	count = 0;
}
If I have 2 things in the queue, I get this error when it calls delete on the second one but the 2 addresses are different. Anyone know how to fix this?
 
Old 02-03-2007, 06:35 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
What are you trying to do? Some general pointers:
1. Learn to love the STL
2. Learn to love the RAII idiom
3. Learn to love gdb
4. Learn to love the C++ FAQ

Last edited by tuxdev; 02-03-2007 at 06:36 PM.
 
Old 02-03-2007, 06:42 PM   #3
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Original Poster
Rep: Reputation: 15
Well I'm not allowed to use STL in this class and I tried gdb but it doesn't give me much information and I'm not sure how to interpret what it does give me. What's the RAII idiom? This is what I got in gdb:

Code:
GNU gdb Red Hat Linux (6.3.0.0-1.132.EL4rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...Using host libthread_db library "/lib64/tls/libthread_db.so.1".

(gdb) run
Starting program: /afs/engin.umich.edu/u/l/m/lmvent/eecs-281/proj1/a.out 
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0xffffe000
*** glibc detected *** free(): invalid pointer: 0x0804b094 ***

Program received signal SIGABRT, Aborted.
0xffffe405 in __kernel_vsyscall ()
(gdb) backtrace
#0  0xffffe405 in __kernel_vsyscall ()
#1  0xf7dd57a5 in raise () from /lib/tls/libc.so.6
#2  0xf7dd7209 in abort () from /lib/tls/libc.so.6
#3  0xf7e0971a in __libc_message () from /lib/tls/libc.so.6
#4  0xf7e0ffbf in _int_free () from /lib/tls/libc.so.6
#5  0xf7e1033a in free () from /lib/tls/libc.so.6
#6  0xf7fa09f1 in operator delete () from /usr/lib/libstdc++.so.6
#7  0x08048c1c in Queue::Purge (this=0xffffd7a0) at queue.cpp:26
#8  0x08048cac in ~Queue (this=0xffffd7a0) at queue.cpp:38
#9  0x0804899d in main () at test.cpp:29
 
Old 02-03-2007, 09:06 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
The most important info gdb can give you with a crash is where it is crashing.

Some more RAII links:
http://en.wikipedia.org/wiki/Resourc...Initialization
http://www.parashift.com/c++-faq-lit...html#faq-17.12

Essentially, it just means make the object clean up after itself in the destructor. And remember that objects are destroyed in the reverse order of construction (so member objects should not be cleaned up in its owner object, but automatically by the compiler.

Last edited by tuxdev; 02-03-2007 at 09:27 PM.
 
Old 02-03-2007, 09:51 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
My guess is that you are deleting an element that has already been deleted.

Is the memory deleted anywhere else?
To help yourself could re-initialise the element
[code]delete &array[head];
array[head] = 0;[code]
and check that array[head] is not null before you try and delete it.
 
  


Reply



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
*** glibc detected *** free(): invalid next size (normal): 0x0000000000503e70 *** vbreddy Programming 2 04-10-2006 06:27 PM
*** glibc detected *** free(): invalid next size (normal): 0x0804c050 *** water&sky Linux - General 2 03-03-2006 12:25 PM
*** glibc detected *** free(): invalid next size (normal): 0x0804c050 *** water&sky Linux - Software 1 03-02-2006 08:23 AM
Glibc free() invalid pointer pierre-luc Programming 3 11-13-2005 08:04 PM
OpenOffice.org *** glibc detected *** free(): invalid pointer: 0x41481c94 *** Artanicus Linux - Software 2 02-19-2005 07:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 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