access violation when unallocating a string

E

Eric Boutin

Hi ! I got the fallowing function :

void pushargv(TmpFile in) {
std::string tmp = in.getfilename();
argv.push_back(in.getfilename());
tocleanup.push_back(in.getfilename());}


where argv and tocleanup are vector<string>


I fallowed up the code, and found out that tmp is NOT NULL, however, at the
end of the function, before I get back to the calling function, I get a
access violation (segfault, call it the way you want, a big fat bug), I
searched in the header, and I found out the bug was in xstring, a internal
header for <string> (function name : tidy if it can helps).. I really
doesn't have a clue what's the cause of the problem.. I mean there's
nothing non-standard here.. just a few strings and a vector.. I did that
thousands of time before.. but here I get a bug.. I use VC++ 6.0.. which
is not the best compiler in town for STL and cie.. so it may be a compiler
bug...

thanks a lot !

-Eric
 
D

David White

Eric Boutin said:
Hi ! I got the fallowing function :

void pushargv(TmpFile in) {
std::string tmp = in.getfilename();
argv.push_back(in.getfilename());
tocleanup.push_back(in.getfilename());}


where argv and tocleanup are vector<string>


I fallowed up the code, and found out that tmp is NOT NULL,

tmp is a class object. It can't be null, bug or no bug. A pointer can be
null, but not an object.
however, at the
end of the function, before I get back to the calling function, I get a
access violation (segfault, call it the way you want, a big fat bug), I
searched in the header, and I found out the bug was in xstring, a internal
header for <string> (function name : tidy if it can helps).. I really
doesn't have a clue what's the cause of the problem.. I mean there's
nothing non-standard here.. just a few strings and a vector.. I did that
thousands of time before.. but here I get a bug.

You haven't posted enough code to tell what's wrong. What does
TmpFile::getfilename() look like? Did you mean to pass the TmpFile object by
value? That's unusual for an object representing a file. Normally file
objects are passed around by reference, e.g.,

void pushargv(TmpFile &in) // pass by reference
{
// etc.
}

int main()
{
TmpFile file("SomeFile.txt"); // open file
pushargv(file);
}
I use VC++ 6.0.. which
is not the best compiler in town for STL and cie.. so it may be a compiler
bug...

I doubt it. That compiler is okay for std::string and std::vector as far as
I know. You say that you've used strings and vectors thousands of times
before. If you were using the same compiler, then don't you think the bug is
more likely in your own code?

DW
 
P

P.J. Plauger

Hi ! I got the fallowing function :

void pushargv(TmpFile in) {
std::string tmp = in.getfilename();
argv.push_back(in.getfilename());
tocleanup.push_back(in.getfilename());}


where argv and tocleanup are vector<string>


I fallowed up the code, and found out that tmp is NOT NULL,

NO SURPRISE, since tmp is an object locally declared.
But you give NO IDEA about what in.getfilename() returns,
so it's hard to judge what's going on here.
however, at the
end of the function, before I get back to the calling function, I get a
access violation (segfault, call it the way you want, a big fat bug), I
searched in the header, and I found out the bug was in xstring,

You mean the result of the bug APPEARED in xstring.
a internal
header for <string> (function name : tidy if it can helps).. I really
doesn't have a clue what's the cause of the problem.. I mean there's
nothing non-standard here..

Except TmpFile, and getfilename.
just a few strings and a vector.. I did that
thousands of time before.. but here I get a bug.. I use VC++ 6.0.. which
is not the best compiler in town for STL and cie.. so it may be a compiler
bug...

Or not. We can't know from what you've shown so far. OTOH, many
TENS OF THOUSANDS of programmers have used V6 STL since 1996, and
some have FAILED to get seg faults. Consider...

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
P

Paul

Eric Boutin said:
Hi ! I got the fallowing function :

I fallowed up the code, and found out that tmp is NOT NULL, however, at the
end of the function, before I get back to the calling function, I get a
access violation (segfault, call it the way you want, a big fat bug), I
searched in the header, and I found out the bug was in xstring, a internal
header for <string> (function name : tidy if it can helps)..

I've used VC 6.0 extensively, and have had no problems with vector<string>.
The problem is more than likely your own code. Tracing back to xstring is
just you finding one of the symptoms of the problem that you've introduced
somewhere else in your code. You can easily corrupt a string or vector
object if you are overwriting memory or doing something else that would lead
to undefined behavior.

Paul
 

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

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top