Re: another memory leak, weird`

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Vivienne" <zhoudan.bupt@xxxxxxxxx> wrote in message
news:1190167122.240652.254120@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
I test the below function. memory leak occured ...

void testleak{
std::string a;
std::cin >> a;
_CrtDumpMemoryLeaks();

}

when I input a few charactors, like 10 charactors, no memory leak
occured here. but when I input more charactors, like 20, memory leak
occured!
why?

The string 'a' has not yet gone out of scope and has not been destroyed
by the time you call _CrtDumpMemoryLeaks. Any memory it has allocated on
the heap is (incorrectly) reported as a leak.

It so happens that a particular implementation of std::string you are
using employs so called small string optimization: strings shorter than
a certain limit are stored directly inside the string object itself,
thus not requiring a heap allocation. Memory for longer strings is still
allocated on the heap. That's why you see a false leak report for long
strings but not for short ones.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Memory Leak in VB6 with strings and UDTs
    ... I have found what I think is a memory leak at the core of Visual Basic 6, ... When, in a loop over my arrays, I set BString1 to an element of an array, ... I have tried casting the number to a string with CStr but that doesn't help ... Dim bytearrayAs Byte ...
    (microsoft.public.vb.general.discussion)
  • Re: Is how to lay out the constant string compiler dependent?
    ... memory of the string that you have used except store the address in a ... the memory as the general memory leak. ... a literal array that is modifiable cannot ...
    (comp.lang.c)
  • Re: Simple Regular Expression need
    ... charactors in a string. ... Here you state that everything between the beginning of the string and the end may not contain a,b or c. ... If you want to validate dates stricter I think there are some javescript functions you can rely on. ... I am not familiar with Regular Expression and I am on a tight project ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ctypes: delay conversion from c_char_p to string
    ... way to do this with ctypes? ... # without giving me an opportunity to destroy the original string. ... this technique for getting pointer *is* mentioned in ctypes documentation. ... I believe this will cause a memory leak, ...
    (comp.lang.python)
  • Dispose Unmanaged resources
    ... We all know that in .NET, we don't need to worry about memory leak ... illustrate the step necessary in disposing unmanaged resources. ... public static extern bool COMDbConnection (string connectionString, ...
    (microsoft.public.dotnet.general)