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 01-06-2006, 12:14 PM   #16
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148

The top two are because the objects are passed by value into add. switch the signature to
Code:
void add(foo& f) { v.push_back(f); }
and they will go away

graeme
 
Old 01-06-2006, 12:31 PM   #17
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45


gotcha!

so in the end.. it may be a delete that is going bad that is not being called explicitly..

like you said graeme in the beginning it would be best to be passing by ref, for efficiency if nothing else..
but surely this should still work if you wanted to pass everything by value... ?
 
Old 01-06-2006, 01:22 PM   #18
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I think so although I'd be in favour of pointers but you do need to be careful not to delete then whilst they are still on the vector, as in the implicit delete in the bar class of your example. But the use of new should overcome that.

As for the original problem I suspect it is an implicit delete but I'm not totally certain. Anyway it was fun!

graeme.
 
Old 01-06-2006, 02:17 PM   #19
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
>> Anyway it was fun!

yes sir!



and urzumph,
check out how graemef changed the vector to a vector of ptrs ..

vector<foo*> v;

and how the change affected all the other functions to handle refrences and read from pts out of the vector.. this is truly the correct way to do this type of thing.. way more efficient.. regardless of why the way you were doing it wont work..
though it is a mystery still to me.. as why..

anyhow though.. Good luck with it!
 
Old 01-06-2006, 06:43 PM   #20
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Original Poster
Rep: Reputation: 30
Firstly, thanks everyone for your replies.

Quote:
Originally Posted by xhi
this is truly the correct way to do this type of thing.. way more efficient.. regardless of why the way you were doing it wont work.
While I may well change additem() to take a pointer, the vectors have to store the actual value because the copy outside of the list is overwritten shortly after the call to additem()
(although that probably could be changed)

Quote:
Originally Posted by xhi
i would consider compiling it if you wanted to upload the code..
I have uploaded it now, you can find it at
It's got all the source files as well as my Makefile (which doesn't work that well, but that's another story)
http://urzumph.homelinux.org/CalanderThingy.zip

Now, on to the interesting bit.
I noticed when apt-get updateing that there were new versions of some of the things I was using - gcc, glibc, libstdc++ etc, so I updated and tried again. Now, I am getting the same error (except different number), but in a different place. The error seemed to be coming from the resize function...

Code:
Checking to see if calander toadd is the same calander 0
Checking to see if calander toadd is the same calander 1
toadd passed required checks. adding Calander...
pushed back score
pushed back calander
Reordering BestOfList...
Resizing BestOfList...
*** glibc detected *** double free or corruption (!prev): 0x08054558 ***
Added Item to new listmake: *** [test] Aborted
(plese note that in the old logs, they were Reordering calander and resizing calander, I realised these were misleading and fixed them)

Now, I had some vector.erase() calls in resize, and I'm not sure I got them right, so for the meantime, I simply commented them out, leaving this function :

Code:
void BestOfList::resize() {
	int lastscore = Scores[Scores.size()-1];
	printf("Resizing BestOfList...\n");
	if(Scores.size() < minsize) {return;}
	for(size_t i = Scores.size()-1;i>=minsize;i--) {
		if(Scores.size() < maxsize) {return;}
		if(lastscore < Scores[i]) {
//Scores.erase(Scores.begin()+i+1,Scores.end());
//Calanders.erase(Calanders.begin()+i+1,Calanders.end());
		}
	}
}
(In theory, I needn't have commented them out because Scores.size() should always be less than minsize with this data)

Apparently, this did not stop the problem however.
I am begining to wonder if perhaps stdout needs to be fflush()'d

and lo and behold :
Code:
Adding to list will continue - score >= 0
Checking to see if calander toadd is the same calander 0
Checking to see if calander toadd is the same calander 1
toadd passed required checks. adding Calander...
pushed back score
pushed back calander
Reordering BestOfList...
Resizing BestOfList...
Added Item to new list
*** glibc detected *** double free or corruption (!prev): 0x08054558 ***
So I'll go and add fflush() calls all over the place and try and track down where this error is

(Sorry if this post seems to ramble a bit, I descovered some of the stuff while writing it)

Edit - Tried to fix indentation of code

Last edited by urzumph; 01-06-2006 at 06:50 PM.
 
Old 01-06-2006, 06:47 PM   #21
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
excellent catch! i just downloaded the source and will take a look at it later...
 
Old 01-06-2006, 11:33 PM   #22
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Original Poster
Rep: Reputation: 30
Ok, I managed to isolate it down to a single line again, this time in the main function :
Code:
oldlist = newlist;
where oldlist and newlist are both BestOfLists.
Now, I figgured that from a C++ perspective, the way I had done this was probably quite silly...

Code:
(snip)
BestOfList oldlist;
BestOfList newlist;
(snip)
while(!iscomplete(oldlist.getcalander(0))) {
    for(unsigned int i = 0; i < oldlist.length(); i++) {
        (snip data processing)
    }
    oldlist = newlist;
    newlist.empty();
}
now I realised in C++ with pointers this was silly, because I could simply swap pointers around:

Code:
(snip)
BestOfList *oldlist, *newlist;
oldlist = new BestOfList;
newlist = new BestOfList;
(snip)
while(!iscomplete(oldlist->getcalander(0))) {
    for(unsigned int i = 0; i < oldlist->length(); i++) {
        (snip data processing)
    }
    printnow("Replacing Oldlist with Newlist\n");
    delete oldlist;
    printnow("Oldlist deleted");
    oldlist = newlist;
    newlist = new BestOfList;
}
And the Line it doesn't like is delete oldlist;.
You don't need to do anything special when you delete instances of classes with vectors in them do you?
 
Old 01-06-2006, 11:56 PM   #23
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
>> You don't need to do anything special when you delete instances of classes with vectors in them do you?

no you dont.. well if it is vector of dynamically allocated objects you ofcourse have to delete themm
if you did..

vector<foo*> v;
v.push_back(new foo);

then you would have to delete it from vector.. (still shouldnt give this error..)

otherwise the vector can just die when the object is destructed...

btw i compiled and ran the source you sent and im getting a segfault.. maybe somthing out of range now?

here is the output.. im trying to get another project done for school or i would try to help a little..
Code:
(snipped)
Adding appointment 1
Calander already has appointment in this position. Failing calander.
Scoring Calander....
Fail condition is true. Returning -1
Added Item to new listGetting Calander 1 from BestOfList
Appointment at 0,11
Adding appointment 2
Calander already has appointment in this position. Failing calander.
Scoring Calander....
Fail condition is true. Returning -1
Added Item to new listGetting Calander 1 from BestOfList
Appointment at 0,14
Adding appointment 3
Testing Cat 0 against our Cat
Testing Cat 1 against our Cat
Scoring Calander....
Fail condition is true. Returning -1
Added Item to new listGetting Calander 0 from BestOfList
Segmentation fault
!?
 
Old 01-07-2006, 08:00 AM   #24
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Just to add my view on the best way to use pointers in this vector.

If the vector is to be your primary storage of the object then the approach should be as follows:
  1. Have a pointer to the object
  2. Create the object using the new keyword
  3. Pass the pointer to the method that pushes it onto the vector
  4. Reuse the pointer to create another object (again using new)

When you delete the vector you will need to go through each element and free the object using the delete keyword. This can be done in the destructor of the object that holds the vector. This gives you a consistent way of managing the data that you allocate as well as making the vector itself efficient.

graeme.
 
Old 01-07-2006, 09:03 AM   #25
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
When you delete the vector you will need to go through each element and free the object using the delete keyword.
just a word of caution about this.
say for example you have a vector of int pointers;
std::vector<int*> ints;

you can not do this
Code:
//delete all the pointers
std::vector<int*> ::iterator iter;
//loop through the vector
for(iter = ints.begin(); iter != ints.end(); ++iter)
{
     delete (*iter);//recover the memory
     ints.erase(iter);//remove from vector
}
the reason for this is that you are removing the iter and then incrementing an iter which no longer is valid.

theres a couple of ways of actually doing this correct, heres two
Code:
//delete all the pointers
std::vector<int*> ::iterator iter;
//loop through the vector
for(iter = ints.begin(); iter != ints.end(); ++iter)
     {delete (*iter);}//recover the memory

ints.erase(ints.begin(),ints.end());//clean the vector

Code:
while( ! ints.empty() )
{
     delete ints.back();//recover the memory
     ints.pop_back();//remove from vector
}

Last edited by dmail; 01-07-2006 at 09:18 AM.
 
Old 01-07-2006, 04:03 PM   #26
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by xhi
btw i compiled and ran the source you sent and im getting a segfault.. maybe somthing out of range now?
That is really wierd. I downloaded and tested the source myself and I got the double-free. If all 3 calanders fail, as per your log (and they should not! assuming you were using the default data) Then yes, it is likely there will be an out of bounds error in one of the vectors, probably causing the segfault.
 
  


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 *** malloc() / free()/ double RohanShrivastav Programming 12 10-01-2012 10:08 AM
FC3 glibc detected *** double free or corruption: josedragone Fedora 5 09-17-2009 10:16 PM
*** glibc detected *** double free or corruption (!prev): 0x082c1120 *** eXor Slackware 6 04-11-2008 08:47 AM
glibc detected ; memory corruption pingu Linux - Software 3 10-12-2005 01:29 AM
Open Office user install error: *** glibc detected *** double free or corruption: 0xb r_jensen11 Linux - General 6 01-16-2005 06:08 AM

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

All times are GMT -5. The time now is 11:44 AM.

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