Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

A little "garbage collector" for your project

, 1 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Combining a smart pointer with a collection we can get all the functionality that the original author wrote a custom class for, and a bit more:typedef std::vector> garbage_collector;Er, that's it. So how would you use this? Say you wanted to use character arrays as...
Combining a smart pointer with a collection we can get all the functionality that the original author wrote a custom class for, and a bit more:
 
typedef std::vector<std::shared_ptr<void>> garbage_collector;
 
Er, that's it. So how would you use this? Say you wanted to use character arrays as the author's examples:
 
garbage_collector gc;
std::shared_ptr<char> ptr( new char[128], std::default_deleter<char[]>() );
gc.push_back( ptr );
 
In this example ptr is the thing you'd use to do something with the memory you've allocated. When the memory's finished with ptr and gc go out of scope and everything's cleaned up.
 
Unlike the original tip this can manage "real" objects with destructors as the pointers don't have their types erased, just their interfaces. It can mix array and non-array types.
 
So everything's peachy? Actually no. Like the original tip this code is completely and utterly pointless. In both cases you're writing too much code.
 
In both cases instead of using raw pointers and manually managing memory there is a far simpler solution - use smart pointers and collections to manage your data. So instead of needing a baroque technique to avoid leaking memory when allocating arrays of characters just reach for a vector:
 
char *p = new char[ 128 ];
 
becomes:
 
std::vector<char> buff( 128 );
 
and that really is it. There's no need to palm the allocated memory off on another auxilary data structure. There's nothing for the programmer to keep track of and less ways of blowing the program up.
 
So if a programmer you work with comes and tells you about an exciting and "safe" way of avoiding memory leaks you can give him the same answer I do: "Memory leak? Oh, that's so last century! I haven't had one of those since 1999."

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Aescleal
Software Developer (Senior)
United Kingdom United Kingdom
I've been programming since 1985 - starting with Fortran 77, then moving onto assembler, C and C++ in about 1991. I also know enough Java and Python to read code but you probably wouldn't want me writing it.
 
I've worked in a wide variety of application areas - defense, banking, games and security with the longest stints being in security. I also seem to end up programming devices far too often.
Follow on   Google+

Comments and Discussions

 
GeneralReason for my vote of 5 I'm glad you stepped in to show the ... PinmemberGordon Brandly12-Aug-10 10:08 
GeneralReason for my vote of 5 Really a good approach to the proble... PinmemberSauro Viti6-Aug-10 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 1 Aug 2010
Article Copyright 2010 by Aescleal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid