string passed by value deleted before usage?

N

nusch

I've problem with mini version control system, quickly reviewing -
class Repository has list of pointers to class Revision, class
Revision has map<string, Patch*> which maps filename to patch.
Patch derrives from File, File has vector<string> lines.

Here all the files, sory but I can't seperate the problem

main.cpp http://pastebin.com/f83YA1G6
file.h http://pastebin.com/0gUiRwz6
repo.h http://pastebin.com/RxX94RXJ
file.cpp http://pastebin.com/7wrQrBtn
repo.cpp http://pastebin.com/wwkUJ1nk
revision.h http://pastebin.com/nTMu9yRx
revision.cpp http://pastebin.com/aq5J29Cg
LongestCommonSubsequence.hpp http://pastebin.com/HHmKmCF1


Whole logic seems to be ok, I've locked copy constructor as explicit
to prevent temporary objects, all objects are created by new, although
somewhat some memory is deleted before (file.cpp line 200)


void File::add_line(string line) {
lines.push_back(line);
}


valgrind shows that info:

==29517== Invalid read of size 8
==29517== at 0x405678: std::vector<std::string,
std::allocator<std::string> >::push_back(std::string const&)
(stl_vector.h:735)
==29517== by 0x4037AE: File::add_line(std::string) (file.cpp:200)
==29517== by 0x40C32C: Revision::parse() (revision.cpp:35)
==29517== by 0x40944D: Repo::load() (repo.cpp:65)
==29517== by 0x409818: Repo::commit(std::string) (repo.cpp:109)
==29517== by 0x40970E: Repo::commit() (repo.cpp:97)
==29517== by 0x408997: main (main.cpp:58)
==29517== Address 0x10 is not stack'd, malloc'd or (recently) free'd
==29517==
==29517==
==29517== Process terminating with default action of signal 11
(SIGSEGV)
==29517== Access not within mapped region at address 0x10
==29517== at 0x405678: std::vector<std::string,
std::allocator<std::string> >::push_back(std::string const&)
(stl_vector.h:735)
==29517== by 0x4037AE: File::add_line(std::string) (file.cpp:200)
==29517== by 0x40C32C: Revision::parse() (revision.cpp:35)
==29517== by 0x40944D: Repo::load() (repo.cpp:65)
==29517== by 0x409818: Repo::commit(std::string) (repo.cpp:109)
==29517== by 0x40970E: Repo::commit() (repo.cpp:97)
==29517== by 0x408997: main (main.cpp:58)


It's points line with push_back, but if I passing string by value
what can cause that the string inside function is deleted ? Or what
line in backtrace is wrong?
 
B

Bo Persson

nusch said:
I've problem with mini version control system, quickly reviewing -
class Repository has list of pointers to class Revision, class
Revision has map<string, Patch*> which maps filename to patch.
Patch derrives from File, File has vector<string> lines.

Here all the files, sory but I can't seperate the problem

main.cpp http://pastebin.com/f83YA1G6
file.h http://pastebin.com/0gUiRwz6
repo.h http://pastebin.com/RxX94RXJ
file.cpp http://pastebin.com/7wrQrBtn
repo.cpp http://pastebin.com/wwkUJ1nk
revision.h http://pastebin.com/nTMu9yRx
revision.cpp http://pastebin.com/aq5J29Cg
LongestCommonSubsequence.hpp http://pastebin.com/HHmKmCF1


Whole logic seems to be ok, I've locked copy constructor as explicit
to prevent temporary objects, all objects are created by new,
although somewhat some memory is deleted before (file.cpp line 200)


void File::add_line(string line) {
lines.push_back(line);
}


valgrind shows that info:

==29517== Invalid read of size 8
==29517== at 0x405678: std::vector<std::string,
std::allocator<std::string> >::push_back(std::string const&)
(stl_vector.h:735)
==29517== by 0x4037AE: File::add_line(std::string) (file.cpp:200)
==29517== by 0x40C32C: Revision::parse() (revision.cpp:35)
==29517== by 0x40944D: Repo::load() (repo.cpp:65)
==29517== by 0x409818: Repo::commit(std::string) (repo.cpp:109)
==29517== by 0x40970E: Repo::commit() (repo.cpp:97)
==29517== by 0x408997: main (main.cpp:58)
==29517== Address 0x10 is not stack'd, malloc'd or (recently)
free'd ==29517==
==29517==
==29517== Process terminating with default action of signal 11
(SIGSEGV)
==29517== Access not within mapped region at address 0x10
==29517== at 0x405678: std::vector<std::string,
std::allocator<std::string> >::push_back(std::string const&)
(stl_vector.h:735)
==29517== by 0x4037AE: File::add_line(std::string) (file.cpp:200)
==29517== by 0x40C32C: Revision::parse() (revision.cpp:35)
==29517== by 0x40944D: Repo::load() (repo.cpp:65)
==29517== by 0x409818: Repo::commit(std::string) (repo.cpp:109)
==29517== by 0x40970E: Repo::commit() (repo.cpp:97)
==29517== by 0x408997: main (main.cpp:58)


It's points line with push_back, but if I passing string by value
what can cause that the string inside function is deleted ?
No.

Or what
line in backtrace is wrong?

You seem to have file.cpp twice, but not repo.cpp, so we cannot see
what repo->commit() does. Something interesting perhaps?


Bo Persson
 
F

Francesco S. Carta

I've problem with mini version control system, quickly reviewing -
class Repository has list of pointers to class Revision, class
Revision has map<string, Patch*> which maps filename to patch.
Patch derrives from File, File has vector<string> lines.

Here all the files, sory but I can't seperate the problem

main.cpp http://pastebin.com/f83YA1G6
file.h http://pastebin.com/0gUiRwz6
repo.h http://pastebin.com/RxX94RXJ
file.cpp http://pastebin.com/7wrQrBtn
repo.cpp http://pastebin.com/wwkUJ1nk
revision.h http://pastebin.com/nTMu9yRx
revision.cpp http://pastebin.com/aq5J29Cg
LongestCommonSubsequence.hpp http://pastebin.com/HHmKmCF1


Whole logic seems to be ok, I've locked copy constructor as explicit
to prevent temporary objects, all objects are created by new, although
somewhat some memory is deleted before (file.cpp line 200)


void File::add_line(string line) {
lines.push_back(line);
}


valgrind shows that info:

==29517== Invalid read of size 8
==29517== at 0x405678: std::vector<std::string,
std::allocator<std::string> >::push_back(std::string const&)
(stl_vector.h:735)
==29517== by 0x4037AE: File::add_line(std::string) (file.cpp:200)
==29517== by 0x40C32C: Revision::parse() (revision.cpp:35)
==29517== by 0x40944D: Repo::load() (repo.cpp:65)
==29517== by 0x409818: Repo::commit(std::string) (repo.cpp:109)
==29517== by 0x40970E: Repo::commit() (repo.cpp:97)
==29517== by 0x408997: main (main.cpp:58)
==29517== Address 0x10 is not stack'd, malloc'd or (recently) free'd
==29517==
==29517==
==29517== Process terminating with default action of signal 11
(SIGSEGV)
==29517== Access not within mapped region at address 0x10
==29517== at 0x405678: std::vector<std::string,
std::allocator<std::string> >::push_back(std::string const&)
(stl_vector.h:735)
==29517== by 0x4037AE: File::add_line(std::string) (file.cpp:200)
==29517== by 0x40C32C: Revision::parse() (revision.cpp:35)
==29517== by 0x40944D: Repo::load() (repo.cpp:65)
==29517== by 0x409818: Repo::commit(std::string) (repo.cpp:109)
==29517== by 0x40970E: Repo::commit() (repo.cpp:97)
==29517== by 0x408997: main (main.cpp:58)


It's points line with push_back, but if I passing string by value
what can cause that the string inside function is deleted ? Or what
line in backtrace is wrong?

I think you're asking for a bit too much. First, lots of people will
discard your message at once just for the fact that you didn't post your
code in the message, not to speak about the amount of code you're
"submitting" for review.

Track down the problem to its very core, add watchers, step through the
debugger, find a way to post equivalent _failing_ code and post it in
the body of your message.

Chances are that, on your way towards posting an acceptable question
along with your code, you will be able to solve your problem by yourself
- read the C++ FAQ for details on how to post your questions here -
shall you not succeed, by all means go ahead and post your circumscribed
failing code here, we will review it.
 
N

nusch

I think you're asking for a bit too much. First, lots of people will
discard your message at once just for the fact that you didn't post your
code in the message, not to speak about the amount of code you're
"submitting" for review.

Track down the problem to its very core, add watchers, step through the
debugger, find a way to post equivalent _failing_ code and post it in
the body of your message.

Chances are that, on your way towards posting an acceptable question
along with your code, you will be able to solve your problem by yourself
- read the C++ FAQ for details on how to post your questions here -
shall you not succeed, by all means go ahead and post your circumscribed
failing code here, we will review it.

I couldn't seperate this bug because of its nature, if it would be
syntax problem or logic of application , I could generate one small
source, but the place where exactly is the error is what I'm actually
asking for so pasting only part of code could hide the memory
coruuption.

The error about 'invalid read' from valgrind is the only error shown
by this tool. So there are not any 'invalid write' and any "new" which
could overwrite segfaulting memory region. So what else could cause
coruption?

Here are repo cpp: http://pastebin.com/GsBusbXx and most important
methods:

File* Repo::get_file(string file_name, unsigned int rev_id) {
File* f = new File(file_name);
if(rev_id>revisions.size()) {
cout << "Invalid revision number" << endl;
return f;
}
unsigned int rev_i = 0;
list<Revision*>::iterator iter = revisions.begin();
while(++rev_i<=rev_id) {
cout << "DEBUG: " << file_name << endl;

Patch* tmp = (*iter)->patch_per_file_map[file_name];

tmp->lines.begin();
cout said:
lines.begin();
Patch* p1 = (*iter++)->patch_per_file_map[file_name];
f->patch(*p1);
}
return f;
}

void Repo::commit(string desc) {
ifstream file("repo.vcs");
if(!file.is_open()) {
cout << "No repository in current directory found, creating
new.." << endl;
init_new();
return;
}
file.close();

load();
int* eee = new int[2];
delete[] eee;
Revision* new_rev = new Revision(revisions.size()+1);
map<string, Patch*>::iterator iter;
list<string>::iterator fnames_iter;
int* fff = new int[2];
delete[] fff;
iter = revisions.back()->patch_per_file_map.begin();
int* ggg = new int[2];
delete[] ggg;
cout << iter->first << endl;
int* hhh = new int[2];
delete[] hhh;
while(iter!=revisions.back()->patch_per_file_map.end())
versioned_files.push_back(iter++->first);
check_new_files();

fnames_iter = versioned_files.begin();
while(fnames_iter!=versioned_files.end()) {
string fname = *fnames_iter;

File disk_file(fname);
disk_file.read_from_file(fname);
cout << fname << endl;

cout << revisions.size() << endl;
File* latest_repo = get_file(fname, revisions.size());

new_rev->patch_per_file_map[fname] = latest_repo-
get_diff(&disk_file);
++fnames_iter;
}

ofstream outfile ("repo.vcs", ios::app);
outfile << "[[[[[rev" << revisions.size()+1 << "]]]]]\n";
outfile << new_rev->to_str();
outfile << "[[[[[-]]]]]" << endl;
outfile.close();
revisions.push_back(new_rev);
//delete new_rew;

}

void Repo::load() {
repo_file = new File("repo.vcs");
vector<string>::iterator i = repo_file->lines.begin();
while(i!=repo_file->lines.end()) {
cout << *i << endl;
unsigned int rev_id;


if(!i->find("[[[[[")) { //current line is begining of revisin
string tmp = (*i).substr(5,i->length()-10);
if(tmp!="-") {
tmp = tmp.substr(3);
istringstream myStream(tmp);
myStream >> rev_id;// endl;//8, i->size()-13); //i-
length()-13;
add_revision(rev_id);
} else
revisions.back()->parse(); //if all lines from current
revision was iterated, further processing - dividing revision to
files is passed to Revsion class
}
else { //current line is content of revision
revisions.back()->feed(*i);
}
++i;
}
//cout << "
//if line = [[[
//while
//revisions.push_back(Revision()
}
 
F

Francesco S. Carta

I couldn't seperate this bug because of its nature, if it would be
syntax problem or logic of application , I could generate one small
source, but the place where exactly is the error is what I'm actually
asking for so pasting only part of code could hide the memory
coruuption.

The error about 'invalid read' from valgrind is the only error shown
by this tool. So there are not any 'invalid write' and any "new" which
could overwrite segfaulting memory region. So what else could cause
coruption?

Here are repo cpp: http://pastebin.com/GsBusbXx and most important
methods:

I've gone that far to download, rename and put together all the files
you linked just to try compiling them and discovering that you omitted
the "differ.h" file.

Believe me, other people aren't nearly that patient.

Now you could post a link to the missing header, and I could have a
further try, but consider that I'm one of the lesser knowledgeable
people of this newsgroup.

If you really need the help of this group to solve the issue, please
make the effort of merging the code in a single file and post it in the
body of your message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top