Need help/hint of resolving invalid memory access
Hi, I know that the following error is caused by invalid memory access in my program... but how do I pinpoint the exact location? ==4384== Invalid read of size 4 ==4384== at 0x4052B855: std::string::assign(std::string const&) (in /usr/lib/libstdc++.so.5.0.4) ==4384== by 0x804F576: TopBuilder::readHost(char const*, PPool&) (/usr/include/g++/bits/basic_string.h:358) ==4384== by 0x804EAB1: TopBuilder::buildNet(std::string const&, PPool&) (TopBuilder.cpp:140) ==4384== by 0x804A0FF: main (main.cpp:59) ==4384== by 0x405A3856: __libc_start_main (in /lib/libc.so.6) ==4384== by 0x8049ED0: (within /home/cincai/tmp/gsim/src/rrsched/rrsched) ==4384== Address 0x0 is not stack'd, malloc'd or free'd So far I've found that this is caused by the combination of access to map and vector. Problem is, I created a separate testing and it works. Afterall, the suspicious statements are simply "m[name] = &factory" and "field.push_back(c)", where "m" is map<string, const Factory*> and "field" is vector<string>. To be more precise: ====================== int main() { m[name] = &factory; ... char line[] = ...; vector<string> field; tokenizer(line, field); } void tokenizer(const char line[], const vector<string>& field) { for (..) { char *c = new char[size]; strncpy(c, ...); field.push_back(c); delete [] c; } field.push_back(...); //last statemetn } ====================== As soon as the last statement (in tokenizer), segfault happened. The funny thing is, if I commented out "field.push_back" inside the loop, segfault wouldn't happen. There must be another invalid memory access, but how do I trace it :(( ? TIA. -- -- Verdi March --
Cincai Patron wrote:
void tokenizer(const char line[], const vector<string>& field) { for (..) { char *c = new char[size]; strncpy(c, ...); field.push_back(c); delete [] c; } field.push_back(...); //last statemetn }
How did you get this to compile at all? The compiler should have barfed at the first field.push_back() because field is constant. I suspect the problem is being caused well away from where it's appearing. Segfaults tend to do that. The nearest suspect I see is strncpy. I think field.push_back(c); delete [] c; is OK because this should get converted implicitly to field.push_back(std::string(c)), which copies c before it's deleted. If you're trying to read a line of data and separate it into tokens, there are some useful functions in iostream or the standard template library. If I recall correctly, something like the following will work std::copy( std::istream_iterator<std::string>( std::cin ), std::istream_iterator<std::string>(), std::back_inserter( field ) ); -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
On Friday 11 July 2003 05:12, John Lamb wrote:
Cincai Patron wrote:
void tokenizer(const char line[], const vector<string>& field) {
How did you get this to compile at all? The compiler should have barfed at the first field.push_back() because field is constant.
ops sorry, mistype. Vector field should not be constant.
If you're trying to read a line of data and separate it into tokens, there are some useful functions in iostream or the standard template library. If I recall correctly, something like the following will work
std::copy( std::istream_iterator<std::string>( std::cin ), std::istream_iterator<std::string>(), std::back_inserter( field ) );
I'll try that. Thanks. -- -- Verdi March --
participants (2)
-
Cincai Patron
-
John Lamb