about STL Memory leak!

Posted on 2002-06-06
Last Modified: 2013-12-14
 When use MSVC6 to complile programms with STLPort, I found MEMORY LEAK. And I saw FAQ on, it says Bounds Checker is wrong. I donnot know which is right, I must use STL to write a service programm.

 will STLPort or SGI's STL occure memory leak?
Question by:clarkent
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 22

Expert Comment

ID: 7058873
The link you pasted does not seem to work.

The boundschecker problem I am aware of is definitely a "problem" with bounds checker.   (Its not a serious problem).  bound checker will report 1 memory leak in the program that is not really a leak it is memory used by the VC RTL.  I am not sure this is the problem you are referring to.

I think its unlikely that any well-written STL will have a memory leak.  

Expert Comment

ID: 7059421
nietod, the FAQ is here:

And says bounds checker is wrong in a certain case.  It will certainly be referring to SGI's implementation of the STL.

>>I think its unlikely that any well-written STL will have a memory leak.
I thought nietod's point was worth restating.  The STL is widely used, even the implementation that came with MSVC++.  It works - I use it :-)

For more information, it might be worth posting the specific error message, although I think it is unlikely you will find anyone here who is familiar enough with the STLPort and BoundsChecker to point you in the exact direction of the detected "error".
LVL 22

Expert Comment

ID: 7059507
The memory leaks discussed in the FAW are not a concern.   They are not really leaks.  This is a common method of optimization.   Many STL implimentatiosn do this.  There are lots of STL classes that repeatedly allocate and delete data of the same size.  (A map for example may need to be adding and deleting lots of nodes as the map is changed.)  These allocations and deletions can be inefficient and can lead to a lot of memory fragmentation.   Many implimentations will not delete the memory when it is no longer needed, instead they store a record of it somewhere (usually in a linked list, and the memory itself stores the linked list so it is very space efficient.).  Then when a block is needed again it just grabs it from the list.  This is much faster than forcing the system to find a block.  (Because the STL stores blocks of exactly the right size so it can take the first one in the list, whereas the RTL woudl have to seach the heap for a block of about the right size.)

so I woudl not worry about this at all.
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Author Comment

ID: 7060735
Thanks a lot to everyone! Below is an test programm.
#include <CrtDbg.h>
bool FuncA();
int main()
  return 1;

bool FuncA()
  string myString="abc";
  return true;
// --------------------- Bounds Checker output:
Memory leak: 320 bytes allocated by HeapAlloc in sbheap.c(102) HANDLE:0X02151E90
Memory leak: 1328 bytes allocated by operator new in ..\\stlport\\stl\\_alloc.c(270) HANDLE:0X02156EC0
Memory leak: 448 bytes allocated by operator new in ..\\stlport\\stl\\_alloc.c(224) HANDLE:0X02161540
//---------------------- _CrtDumpMemoryLeaks output:
Detected memory leaks!
Dumping objects ->
{69} normal block at 0x02162C90, 144 bytes long.
 Data: <tsK             > 74 73 4B 00 00 00 00 00 00 00 00 00 00 00 00 00
{68} normal block at 0x02162D50, 144 bytes long.
 Data: <tsK             > 74 73 4B 00 00 00 00 00 00 00 00 00 00 00 00 00
{67} normal block at 0x02162E10, 144 bytes long.
 Data: <tsK             > 74 73 4B 00 00 00 00 00 00 00 00 00 00 00 00 00
{66} normal block at 0x02162ED0, 144 bytes long.
 Data: <tsK             > 74 73 4B 00 00 00 00 00 00 00 00 00 00 00 00 00
{65} normal block at 0x02162F90, 92 bytes long.
 Data: < sK  ?L  /      > EC 73 4B 00 F0 3F 4C 00 BC 2F 16 02 BF BF BF BF
{64} normal block at 0x02161040, 92 bytes long.
 Data: < sK  ?L l       > EC 73 4B 00 F0 3F 4C 00 6C 10 16 02 BF BF BF BF
{63} normal block at 0x021610D0, 92 bytes long.
 Data: < sK  ?L         > EC 73 4B 00 D0 3F 4C 00 FC 10 16 02 BF BF BF BF
{62} normal block at 0x02161160, 92 bytes long.
 Data: <(tK  ?L         > 28 74 4B 00 B0 3F 4C 00 8C 11 16 02 BF BF BF BF
{61} normal block at 0x021611F0, 796 bytes long.
 Data: <September       > 53 65 70 74 65 6D 62 65 72 00 BF BF BF BF BF BF
{60} normal block at 0x02161540, 452 bytes long.
 Data: <Thu mbe Wed mbe > 54 68 75 00 6D 62 65 00 57 65 64 00 6D 62 65 00
{59} normal block at 0x02156EC0, 1332 bytes long.
 Data: <%a %b %e %H:%M:%> 25 61 20 25 62 20 25 65 20 25 48 3A 25 4D 3A 25
{58} normal block at 0x02161730, 348 bytes long.
 Data: <PM      Wed M:% > 50 4D 00 CD CD CD CD CD 57 65 64 00 4D 3A 25 00
{57} normal block at 0x021618C0, 324 bytes long.
 Data: <C       Sun     > 43 00 CD CD CD CD CD CD 53 75 6E 00 CD CD CD CD
Object dump complete.
LVL 22

Accepted Solution

nietod earned 75 total points
ID: 7060929
You can probably ignore the leaks reported by _crtDumpMemoryLeaks.  That function is useful only after the RTL has cleaned up, whick does not occur until after main() ends and all the static variables are destroyed.  i.e. you are clalling it much too soon.

You can _CrtSetDbgFlg() to have the RTL call it at the appropriate time..

I suspect the problems reported by bounds checker are not a problem--but there is no way to be sure.

If you have dobuts, you can use the STL that comes with VC.  That doesn't have any serious memory leak problems--none I've ever encountered.

Author Comment

ID: 7061011
nietod,Thank you. I must use the thread safe STL and it running as a service. Can you suggest me more?
It is my first time ask question here.
LVL 22

Expert Comment

ID: 7061687
The VC STL is thread safe.  (I use it in multiple threads.)   At least, it is safe if you take precautions to synchorize the objects your create with it.  i.e. if you share an STL string between threads, you need to synchronize access to that  to that STL string.   However, if you create sperate STL strings in seperate threads, you don't need to worry about them--even if the STL strings share data between threads.

I'm not sure what sort of thread safety SGI is providing, but I doubt it is any more or less than this.  Is there a reason you chose this STL?

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question