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

Started by
7 comments, last by Wymsical 20 years, 8 months ago
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
Advertisement
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?
Yeah source would definitely help a lot
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]
What runtime library are you using for the dll?
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.
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
Use debug multithreaded DLL across the entire solution.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:Original post by Wymsical
i''m kinda stuck with stlport


why would that be a problem? stlport is very good.

This topic is closed to new replies.

Advertisement