G
Guest
Hello,
I'm new to the boards, and I've been struggling with a problem porting my
company's code from Visual C++ 6.0 to Visual C++ 2005.
We've found some crashes that I've traced to the 'c_str()' member of the
standard string class. I've debugged into the deepest layers of the system
code and cannot explain this.
What we have is a class with a private 'string' member:
string _linkType;
And public function that returns it:
class Link
....
string getLinkType() const {return _linkType; }
When attempting to access the string as a char * later in the code, the
resulting string "1M" does not get extracted properly:
Link *ilink;
const char *pLinkType;
pLinkType = ilink->getLinkType().c_str();
The result of this is that pLinkType is "".
Now - here's the weird part.
const char *blah;
string strvalue;
strvalue = ilink->getLinkType();
blah = strvalue.c_str();
If I do this, 'blah' contains the proper string '1M'.
How can this be? If I break apart the operation into two lines, it works.
Keep it as one line and it fails!?
I've tried defining the member as std::string and return value from the get
function as std::string as well, with no change in behavior. I'd really
like a good solution to this, as this example literally shows up hundreds of
times in our code. It would be a lot of work to make the massive changes to
ensure the proper value is getting returned.
This works just fine in Visual C++ 6.0. I've even installed VC++2005 SP1 to
see if it was fixed there, with no change.
Thanks so much in advance for your help!
I'm new to the boards, and I've been struggling with a problem porting my
company's code from Visual C++ 6.0 to Visual C++ 2005.
We've found some crashes that I've traced to the 'c_str()' member of the
standard string class. I've debugged into the deepest layers of the system
code and cannot explain this.
What we have is a class with a private 'string' member:
string _linkType;
And public function that returns it:
class Link
....
string getLinkType() const {return _linkType; }
When attempting to access the string as a char * later in the code, the
resulting string "1M" does not get extracted properly:
Link *ilink;
const char *pLinkType;
pLinkType = ilink->getLinkType().c_str();
The result of this is that pLinkType is "".
Now - here's the weird part.
const char *blah;
string strvalue;
strvalue = ilink->getLinkType();
blah = strvalue.c_str();
If I do this, 'blah' contains the proper string '1M'.
How can this be? If I break apart the operation into two lines, it works.
Keep it as one line and it fails!?
I've tried defining the member as std::string and return value from the get
function as std::string as well, with no change in behavior. I'd really
like a good solution to this, as this example literally shows up hundreds of
times in our code. It would be a lot of work to make the massive changes to
ensure the proper value is getting returned.
This works just fine in Visual C++ 6.0. I've even installed VC++2005 SP1 to
see if it was fixed there, with no change.
Thanks so much in advance for your help!