Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

A little "garbage collector" for your project

, 26 Jul 2010
Rate this:
Please Sign up or sign in to vote.
A little class to help you avoid memory leaks
How long did you debug your application to find a memory leak?
 
Now, i think that I found a little solution to this problem. Here is it.
 
CGarbageCollector.cpp
#include "CGarbageCollector.h"
void* CGarbageCollector::addNewPointer(void * ptr)
{
    this->_ptrs.push_back(ptr);
    return ptr;
}
void CGarbageCollector::freePointers()
{
    for(int i=0;i<this->_ptrs.size();i++)
    {
        if(this->_ptrs[i] != NULL)
        {
            if(this->collectortype == true)
                delete [] this->_ptrs[i];
            else
                delete this->_ptrs[i];
            this->_ptrs[i] = NULL;
        }
    }
    this->_ptrs.clear();
}
CGarbageCollector::~CGarbageCollector()
{
    this->freePointers();
}
 

CGarbageCollector.h
 
#ifndef CGARBAGE_COLLECTOR_H_
#define CGARBAGE_COLLECTOR_H_
 
#include <vector>

class CGarbageCollector
{
public:
 
	/*Add new pointer into GarbageCollector's array.
	  Param: memory pointer
	  Returns: memory pointer (param) ***
	  REMEMBER TO AVOID USING DELETE FUNCTION INTO YOUR FUNCTIONS, IF YOU USE THIS GARBAGE COLLECTOR ****/
	void* addNewPointer(void * ptr);
 
	/*Delete all pointers allocated, and erase all object's allocated pointers.*/
	void freePointers();
 
	/*Set to true if you've allocated an array into heap (new void [20])
	  Set to false if you've allocated a simple variable into heap (new void)*/
	void setCollectorType(bool type) { collectortype = type;}
 
	CGarbageCollector(){collectortype = true;}
	/*Will call freePointers() function*/
	~CGarbageCollector();
private:
	bool collectortype;
	std::vector<void*> _ptrs;	
};
#endif
 
This class is very simple to use.
1) You need to declare an istance of CGarbageCollector class
2) Whenever you allocate a pointer, you must call addNewPointer function.
 
Important Notes:

- The function returns the same pointer, passed as parameter.
- The function DOESN'T allocate the pointer.
- If you use this class, you must remember to avoid using delete construct.
 
Example:

[...]
 
char * myFunc()
{
  char * foo = new char [10];
  return foo;
}
int main()
{
 CGarbageCollector collector;
 char * hello = new char [20];
 collector.addNewPointer(hello);
 char * hi = (char*) collector.addNewPointer(myFunc());
 
 collector.freePointers(); //You can also use the destructor

}</br>


License

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

Share

About the Author

SpaceSoft
Software Developer (Junior) Space Softwares
Italy Italy
We're born in the 2008. From the 2010, we're working for fun at an FPS opensource project.
This isn't a commercial company, is only a group created for fun by some teens

Comments and Discussions

 
GeneralReason for my vote of 5 good post Pinmemberkoolprasad20033-Aug-10 4:09 
GeneralReason for my vote of 2 This really is a solution in search ... PinmemberAescleal31-Jul-10 11:53 

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.140916.1 | Last Updated 26 Jul 2010
Article Copyright 2010 by SpaceSoft
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid