RAII / RIIA best-practices question about std::vector
-
So one of my (many) recent questions ellicited a link to Resource Acquisition Is Initialization[^]. The wikipedia entry made sense and it seemed like, in general, this structure would be a good idea. So now I'm using
std::vector
for the first time since my RAII introduction, and I recall thatvector<>
leaks unless I callvector<>.clear()
when I'm done with the vector. Does that mean, considering RAII, that whenever I want to usestd::vector
the best-practice method would be to write a specialized container class that calls.clear()
in the destructor and exposes all thestd::vector
methods? Now that I think of it, the container could be written as a template class.... but I'm going to let the question stand, since I'm just learning about RAII... Is this an exception to the rule, or would a container class be best? A thousand praises to all those who exhibit patience with the noob! MZR -
So one of my (many) recent questions ellicited a link to Resource Acquisition Is Initialization[^]. The wikipedia entry made sense and it seemed like, in general, this structure would be a good idea. So now I'm using
std::vector
for the first time since my RAII introduction, and I recall thatvector<>
leaks unless I callvector<>.clear()
when I'm done with the vector. Does that mean, considering RAII, that whenever I want to usestd::vector
the best-practice method would be to write a specialized container class that calls.clear()
in the destructor and exposes all thestd::vector
methods? Now that I think of it, the container could be written as a template class.... but I'm going to let the question stand, since I'm just learning about RAII... Is this an exception to the rule, or would a container class be best? A thousand praises to all those who exhibit patience with the noob! MZRMike the Red wrote:
I recall that vector<> leaks unless I call vector<>.clear() when I'm done with the vector.
You recall incorrectly :-) vector deallocates and calls the destructor of each contained object when it's destructed.
Mike the Red wrote:
Does that mean, considering RAII, that whenever I want to use std::vector the best-practice method would be to write a specialized container class that calls .clear() in the destructor and exposes all the std::vector methods?
No - just use vector. All STL container classes are (minus trivial bugs) intended to have that behaviour, i.e. they destruct the contained elements and deallocate their storage when destructed.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Mike the Red wrote:
I recall that vector<> leaks unless I call vector<>.clear() when I'm done with the vector.
You recall incorrectly :-) vector deallocates and calls the destructor of each contained object when it's destructed.
Mike the Red wrote:
Does that mean, considering RAII, that whenever I want to use std::vector the best-practice method would be to write a specialized container class that calls .clear() in the destructor and exposes all the std::vector methods?
No - just use vector. All STL container classes are (minus trivial bugs) intended to have that behaviour, i.e. they destruct the contained elements and deallocate their storage when destructed.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
That I wasted the last 4 hours trying to stop this from leaking:
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
vector<char *> vc;
vc.push_back("test");
_CrtDumpMemoryLeaks();
return 0;
}
Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {44} normal block at 0x008A0FF0, 4 bytes long.
Data: < C > A0 A5 43 00
Object dump complete.
The program '[7368] ConTest.exe: Native' has exited with code 0 (0x0).When all I needed was a $#^$#&-ing pair of braces...
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
{
vector<char *> vc;
vc.push_back("test");
}
_CrtDumpMemoryLeaks();
return 0;
}I just love learning curves, don't you? Thank you for your help, Stuart! ...thanks to you, I wasted 4 hours instead of 5 or 6 ;P
-
That I wasted the last 4 hours trying to stop this from leaking:
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
vector<char *> vc;
vc.push_back("test");
_CrtDumpMemoryLeaks();
return 0;
}
Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {44} normal block at 0x008A0FF0, 4 bytes long.
Data: < C > A0 A5 43 00
Object dump complete.
The program '[7368] ConTest.exe: Native' has exited with code 0 (0x0).When all I needed was a $#^$#&-ing pair of braces...
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
{
vector<char *> vc;
vc.push_back("test");
}
_CrtDumpMemoryLeaks();
return 0;
}I just love learning curves, don't you? Thank you for your help, Stuart! ...thanks to you, I wasted 4 hours instead of 5 or 6 ;P
Ummmm - the vector hasn't been destructed when you call _CrtDumpMemoryLeaks... Try this:
#include
#include
#includeint _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );{ vector vc; vc.push\_back("test"); } \_CrtDumpMemoryLeaks(); return 0;
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Ummmm - the vector hasn't been destructed when you call _CrtDumpMemoryLeaks... Try this:
#include
#include
#includeint _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );{ vector vc; vc.push\_back("test"); } \_CrtDumpMemoryLeaks(); return 0;
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-nt-
-
-nt-
Faster than a speeding bullet :-)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p