strstream Memory Leak

Discussion in 'C++' started by nithya4u@gmail.com, Oct 17, 2006.

  1. Guest

    I am working on a c++ module, where large amount of data needs to be
    written to a stream and str() method is been used to assign the value
    of this to the char*. This produces the intended result. But when i run
    purify on it i get the following results.

    [W] MLK: Memory leak of 8656 bytes from 9 blocks allocated in
    strstreambuf::doallocate(void) [Builder.exe]
    Distribution of leaked blocks
    987 bytes from 1 block of 987 bytes (0x03977ea8)
    980 bytes from 1 block of 980 bytes (0x038ff010)
    976 bytes from 1 block of 976 bytes (0x03974cf8)
    969 bytes from 1 block of 969 bytes (0x03969290)
    962 bytes from 1 block of 962 bytes (0x03967a00)
    960 bytes from 1 block of 960 bytes (0x03965d78)
    958 bytes from 1 block of 958 bytes (0x038fe388)
    957 bytes from 1 block of 957 bytes (0x003961f8)
    907 bytes from 1 block of 907 bytes (0x041080b0)
    Allocation location
    new(UINT,int,char const*,int) [dbgnew.cpp:46]
    strstreambuf::doallocate(void) [_strstre.cpp:201]
    strstreambuf::eek:verflow(int) [_strstre.cpp:256]
    streambuf::xsputn(char const*,int) [streamb.cpp:182]
    streambuf::sputn(char const*,int) [.\.\streamb.h:187]
    ostream::writepad(char const*,char const*)
    [ostream.cpp:166]
    ostream::<<(char const*) [ostream.cpp:61]
    writeInvisibleObject(char *,char *,char *,char const*)
    [E:\Builder\src\main.cpp:2499]

    This happens at the line where the char* is been assigned to the
    strstream variable.

    strstream strmvar;
    strmvar << dataChar;

    char* datadetail = strmvar.str();
    delete datadetail;

    Initially i assumed the reason to be writing a char* to the strstream
    variable. But the error occured when i tried with int even. The purify
    results indicate that it if beacuse of a overflow to the strstream var.
    As for as my understanding the new memory that is been allocated was
    never deleted. Since the program involves a huge amount of data this
    could occur. Is the overflow the cause for the leak? Is there a way to
    get rid of this leak? I definitely need to remove this as we are
    running the code in batch mode for a set of input files. and this may
    affect the performance of the application when run continously and may
    lead to out of memory error even.

    Can someone help me?
     
    , Oct 17, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > I am working on a c++ module, where large amount of data needs to be
    > written to a stream and str() method is been used to assign the value
    > of this to the char*. This produces the intended result. But when i run
    > purify on it i get the following results.
    >
    > [W] MLK: Memory leak of 8656 bytes from 9 blocks allocated in
    > strstreambuf::doallocate(void) [Builder.exe]
    > Distribution of leaked blocks
    > 987 bytes from 1 block of 987 bytes (0x03977ea8)
    > 980 bytes from 1 block of 980 bytes (0x038ff010)
    > 976 bytes from 1 block of 976 bytes (0x03974cf8)
    > 969 bytes from 1 block of 969 bytes (0x03969290)
    > 962 bytes from 1 block of 962 bytes (0x03967a00)
    > 960 bytes from 1 block of 960 bytes (0x03965d78)
    > 958 bytes from 1 block of 958 bytes (0x038fe388)
    > 957 bytes from 1 block of 957 bytes (0x003961f8)
    > 907 bytes from 1 block of 907 bytes (0x041080b0)
    > Allocation location
    > new(UINT,int,char const*,int) [dbgnew.cpp:46]
    > strstreambuf::doallocate(void) [_strstre.cpp:201]
    > strstreambuf::eek:verflow(int) [_strstre.cpp:256]
    > streambuf::xsputn(char const*,int) [streamb.cpp:182]
    > streambuf::sputn(char const*,int) [.\.\streamb.h:187]
    > ostream::writepad(char const*,char const*)
    > [ostream.cpp:166]
    > ostream::<<(char const*) [ostream.cpp:61]
    > writeInvisibleObject(char *,char *,char *,char const*)
    > [E:\Builder\src\main.cpp:2499]
    >
    > This happens at the line where the char* is been assigned to the
    > strstream variable.
    >
    > strstream strmvar;
    > strmvar << dataChar;
    >
    > char* datadetail = strmvar.str();
    > delete datadetail;
    >
    > Initially i assumed the reason to be writing a char* to the strstream
    > variable. But the error occured when i tried with int even. The purify
    > results indicate that it if beacuse of a overflow to the strstream var.
    > As for as my understanding the new memory that is been allocated was
    > never deleted. Since the program involves a huge amount of data this
    > could occur. Is the overflow the cause for the leak? Is there a way to
    > get rid of this leak? I definitely need to remove this as we are
    > running the code in batch mode for a set of input files. and this may
    > affect the performance of the application when run continously and may
    > lead to out of memory error even.
    >
    > Can someone help me?


    First, you should use std::stringstreams instead of std::strstreams if
    at all possible. The latter have been deprecated.

    Second, I think your problem might be that you forgot to terminate the
    strstream with std::ends:

    strstream strmvar;
    strmvar << dataChar << ends;

    Cheers! --M
     
    mlimber, Oct 17, 2006
    #2
    1. Advertising

  3. Ron Natalie Guest

    wrote:

    >
    > strstream strmvar;
    > strmvar << dataChar;
    >
    > char* datadetail = strmvar.str();
    > delete datadetail;


    delete [] datadetail;

    I'm surprised Putrify doesn't give you an explicit error on this
    one.
     
    Ron Natalie, Oct 17, 2006
    #3
  4. Guest

    Ron Natalie wrote:
    > wrote:
    >
    > >
    > > strstream strmvar;
    > > strmvar << dataChar;
    > >
    > > char* datadetail = strmvar.str();
    > > delete datadetail;

    >
    > delete [] datadetail;
    >
    > I'm surprised Putrify doesn't give you an explicit error on this
    > one.


    Thanks. Since i am working on Windows probably purify didn't throw an
    error on this.

    I solved the problem that i had stated. That was because of the failure
    to use freeze method. Once i unfreezed the buffer the destructor itself
    took care of the deallocation of the strstream variable.

    strmvar.rdbuf()->freeze(0);

    made me fix the problem. Is this going to be a problem when I port the
    application to UNIX? Do I need to make any changes to make it work?
     
    , Oct 17, 2006
    #4
  5. Sunil Varma Guest

    mlimber wrote:
    > wrote:
    > > I am working on a c++ module, where large amount of data needs to be
    > > written to a stream and str() method is been used to assign the value
    > > of this to the char*. This produces the intended result. But when i run
    > > purify on it i get the following results.
    > >
    > > [W] MLK: Memory leak of 8656 bytes from 9 blocks allocated in
    > > strstreambuf::doallocate(void) [Builder.exe]
    > > Distribution of leaked blocks
    > > 987 bytes from 1 block of 987 bytes (0x03977ea8)
    > > 980 bytes from 1 block of 980 bytes (0x038ff010)
    > > 976 bytes from 1 block of 976 bytes (0x03974cf8)
    > > 969 bytes from 1 block of 969 bytes (0x03969290)
    > > 962 bytes from 1 block of 962 bytes (0x03967a00)
    > > 960 bytes from 1 block of 960 bytes (0x03965d78)
    > > 958 bytes from 1 block of 958 bytes (0x038fe388)
    > > 957 bytes from 1 block of 957 bytes (0x003961f8)
    > > 907 bytes from 1 block of 907 bytes (0x041080b0)
    > > Allocation location
    > > new(UINT,int,char const*,int) [dbgnew.cpp:46]
    > > strstreambuf::doallocate(void) [_strstre.cpp:201]
    > > strstreambuf::eek:verflow(int) [_strstre.cpp:256]
    > > streambuf::xsputn(char const*,int) [streamb.cpp:182]
    > > streambuf::sputn(char const*,int) [.\.\streamb.h:187]
    > > ostream::writepad(char const*,char const*)
    > > [ostream.cpp:166]
    > > ostream::<<(char const*) [ostream.cpp:61]
    > > writeInvisibleObject(char *,char *,char *,char const*)
    > > [E:\Builder\src\main.cpp:2499]
    > >
    > > This happens at the line where the char* is been assigned to the
    > > strstream variable.
    > >
    > > strstream strmvar;
    > > strmvar << dataChar;
    > >
    > > char* datadetail = strmvar.str();
    > > delete datadetail;
    > >
    > > Initially i assumed the reason to be writing a char* to the strstream
    > > variable. But the error occured when i tried with int even. The purify
    > > results indicate that it if beacuse of a overflow to the strstream var.
    > > As for as my understanding the new memory that is been allocated was
    > > never deleted. Since the program involves a huge amount of data this
    > > could occur. Is the overflow the cause for the leak? Is there a way to
    > > get rid of this leak? I definitely need to remove this as we are
    > > running the code in batch mode for a set of input files. and this may
    > > affect the performance of the application when run continously and may
    > > lead to out of memory error even.
    > >
    > > Can someone help me?

    >
    > First, you should use std::stringstreams instead of std::strstreams if
    > at all possible. The latter have been deprecated.
    >
    > Second, I think your problem might be that you forgot to terminate the
    > strstream with std::ends:
    >
    > strstream strmvar;
    > strmvar << dataChar << ends;
    >
    > Cheers! --M


    Once you are done with object of strstream that's strmvar call
    freeze(true) on strmvar.
    strmvar.freeze(true);
    I too had the same problem earlier.
    This worked fine.
    But change the deprecated strstream to stringstream.

    Regards
    Sunil
     
    Sunil Varma, Oct 17, 2006
    #5
  6. mlimber Guest

    wrote:
    > Ron Natalie wrote:
    > > wrote:
    > >
    > > >
    > > > strstream strmvar;
    > > > strmvar << dataChar;
    > > >
    > > > char* datadetail = strmvar.str();
    > > > delete datadetail;

    > >
    > > delete [] datadetail;
    > >
    > > I'm surprised Putrify doesn't give you an explicit error on this
    > > one.

    >
    > Thanks. Since i am working on Windows probably purify didn't throw an
    > error on this.
    >
    > I solved the problem that i had stated. That was because of the failure
    > to use freeze method. Once i unfreezed the buffer the destructor itself
    > took care of the deallocation of the strstream variable.
    >
    > strmvar.rdbuf()->freeze(0);
    >
    > made me fix the problem. Is this going to be a problem when I port the
    > application to UNIX? Do I need to make any changes to make it work?


    As long as you are able to change things, switch to std::stringstreams
    without delay, and your problems will all magically go away! (Ok,
    that's an exaggeration, but it will likely help since memory management
    will be much simpler.)

    Cheers! --M
     
    mlimber, Oct 17, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bpb

    strstream question

    Bpb, Sep 23, 2003, in forum: C++
    Replies:
    1
    Views:
    406
    Ron Natalie
    Sep 23, 2003
  2. vishnu mahendra

    Use of strstream.h?

    vishnu mahendra, Apr 6, 2004, in forum: C++
    Replies:
    2
    Views:
    1,565
    Kevin Goodsell
    Apr 6, 2004
  3. vishnu mahendra

    Queries about strstream.h

    vishnu mahendra, Apr 10, 2004, in forum: C++
    Replies:
    3
    Views:
    4,379
    Buster
    Apr 11, 2004
  4. News For Mohan

    use of strstream object of standard lib

    News For Mohan, Aug 10, 2004, in forum: C++
    Replies:
    1
    Views:
    398
    Victor Bazarov
    Aug 10, 2004
  5. derek
    Replies:
    2
    Views:
    436
    Old Wolf
    Sep 29, 2004
Loading...

Share This Page