Archived

This topic is now archived and is closed to further replies.

Wymsical

std:stringstream and std::tring in a DLL giving me giving me _CrtIsValidHeapPointer

Recommended Posts

Wymsical    122
Hi, one of the dependencies of my program formats a string with a stringstream and returns the string of the stream with .str(); However, i''m getting the typical _CrtIsValidHeapPointer assertion failure. The assertion error occurrs only when the string (being returned by the stringstream::str() function) is being destroyed. But even then its intermittent, it seems that it depends on the length of the string. I find it weird that its giving me this error as i''m not passing teh std::strings around my system by ref or by pointer, so the data should be copied. Also the fact that the size of the string affects the outcome worries me. The class that i''m using is isolated in the DLL i''m using, so its not a conflict in the library. I''m using STLport[4.5.3] so that might give me some issues. Is there some way to to disable this checks for valid heap pointers? and if so, would it be wise to do so? Wallaba

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Wymsical
Is there some way to to disable this checks for valid heap pointers? and if so, would it be wise to do so?
The checks are disable in release builds. It would be *really* stupid to ignore those errors. You have one or more bugs you need to locate. Are you able to reproduce it in a simple example you can post here?

Share this post


Link to post
Share on other sites
Wymsical    122
k,
this is in the dll:

class MyClass {
public:
//// Constructors etc here....

std::string DoSomething();
};

std::string MyClass::DoSomething() {
std::stringstream str;
str << "A very very very very very very very very long string" << std::endl;
str << "A very very very very very very very very long string" << std::endl;
str << "A very very very very very very very very long string" << std::endl;
str << "A very very very very very very very very long string" << std::endl;

return str.str();
}


and this will be in my main program:


void main( void ) {
MyClass m;
std::cout << m.DoSomething() << std::endl;
}


it will give me that assertion error when the string returned by m.DoSomething() gets destroyed.

I admit i'm not too clear on when memory is copied, and allocated when working with strinstreams and strings.

also if i make DoSomething( std::string &sz ) instead and have my main program pass it a reference to as tring that the main program constructed, it still gives me teh error

Wallaba

[edited by - wymsical on August 9, 2003 2:31:51 PM]

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Make a temporary string object from the str() return, then return that.

I think the data is doing something like so (but im not an expert so I''m probably wrong):

str() returns a const string
a temporary const string is created from the returned const string
this gets returned, but since there''s no ''normal'' string here, the data isn''t correctly maintained/balanced, is deallocated, and then when the return attempts to copy the const string, it has invalid data.

using a different STL version might help.

Share this post


Link to post
Share on other sites
Wymsical    122
stringstream::str() returns an std::string, its strstream::str() that returns a char *.

I''m using a Debug Multithreaded.

Is there another stl implementation out there other than stlport that i can use wtih msvc6? (other than msvc6''s stnadard implementation) cuz right now i think i''m kinda stuck with stlport when building on msvc6

Share this post


Link to post
Share on other sites