473,748 Members | 4,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL vector memory leaks

Hi. I was wondering if there were any known leaks using STL vectors.
Also, I was wondering if I have to do any sort of deleting or memory
clearing when im done with the vector, or will they be removed
correctly when they leave scope?

the only memory issue i know is that if i use:

vector.clear()

on objects that have memory allocated within them, then it will cause
leaks. is there anythign else i should know? tnx.

oliver

Jul 23 '05 #1
3 9241
"laniik" <la****@yahoo.c om> wrote
Hi. I was wondering if there were any known leaks using STL vectors.
Also, I was wondering if I have to do any sort of deleting or memory
clearing when im done with the vector, or will they be removed
correctly when they leave scope?

the only memory issue i know is that if i use:

vector.clear()

on objects that have memory allocated within them, then it will cause
leaks. is there anythign else i should know? tnx.


Only if you store pointers to objects then somewhere you need
to free these objects before the vector gets destroyed.
And: if the objects allocate memory themself then it should
be released in their destructors.
Jul 23 '05 #2


laniik schreef:
Hi. I was wondering if there were any known leaks using STL vectors.
Also, I was wondering if I have to do any sort of deleting or memory
clearing when im done with the vector, or will they be removed
correctly when they leave scope?

the only memory issue i know is that if i use:

vector.clear()

on objects that have memory allocated within them, then it will cause
leaks.


No it won't. vector<T>::clea r calls T::~T for every object. That will
prevent memory leaks (assuming, of course, that there are no leaks
in T::~T itself, but vector<> can't fix that for you)

Regards,
Michiel Salters

Jul 23 '05 #3
Just to add that you only need to call vector.clear() if you want to
re-use the vector. A vector that goes out of scope automatically calls
T::~T for every object. As mentioned earlier, if you have a vector of
pointers, then you need to manage that memory yourself.

As per stl source comments, vector.clear() is slow. Do not call it
unless you have to. It recommends using std::list if you need to clear
the vector frequently.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6265
by: Mike Krasnik | last post by:
Hello all, I've been messing with checking memory leaks last time, and it looks like some of the memory was not freed due to the fact "library's default allocators keep free memory in a pool for later reuse, rather than returning it to the OS." In C it can be easily avoided using function extern "C" void __libc_freeres(void);
4
2581
by: The Directive | last post by:
I can't understand why in this folling example the message "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because of polymorphism? How can I rewrite this example using a vector to achieve polymorphism. Help!!! I'm very confused. //Base class. class Base { public: Base()
24
2932
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
6
4462
by: thangamani.vaiyapuri | last post by:
Hi, The below code snippet shows memory issues in Vector's push_back method. #include <iostream.h> #include <vector> class Base {
10
5169
by: vsgdp | last post by:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller than the current size, elements at the end of the container will be destroyed." What does "destroyed" mean? It seems to me that if n is smaller than the current size, simple adjust the internal size counter to n and be done.
9
2308
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); //???? Required ??????????????//
10
2089
by: ma740988 | last post by:
I'm perusing source that quite frankly looks like a disaster in the making. This is a case though where return by reference gets muddled with my understand based on readings. struct bar { double variable; void init() { memset ( this, 0, sizeof ( bar ) ); } bar() { init(); } bool operator <( bar & f ) const { return ( variable > f.variable ) ; }
2
2413
by: andrey.vul | last post by:
code: #include <iostream> #include <vector> #include <stdexcept> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>
8
3618
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can account for all the memory. The question is about std::vector<-- how can I track memory usage
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.