Created
August 4, 2011 09:29
-
-
Save pazdera/1124832 to your computer and use it in GitHub Desktop.
Example of `object pool' design pattern in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Example of `object pool' design pattern | |
* Copyright (C) 2011 Radek Pazdera | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <string> | |
#include <iostream> | |
#include <list> | |
class Resource | |
{ | |
int value; | |
public: | |
Resource() | |
{ | |
value = 0; | |
} | |
void reset() | |
{ | |
value = 0; | |
} | |
int getValue() | |
{ | |
return value; | |
} | |
void setValue(int number) | |
{ | |
value = number; | |
} | |
}; | |
/* Note, that this class is a singleton. */ | |
class ObjectPool | |
{ | |
private: | |
std::list<Resource*> resources; | |
static ObjectPool* instance; | |
ObjectPool() {} | |
public: | |
/** | |
* Static method for accessing class instance. | |
* Part of Singleton design pattern. | |
* | |
* @return ObjectPool instance. | |
*/ | |
static ObjectPool* getInstance() | |
{ | |
if (instance == 0) | |
{ | |
instance = new ObjectPool; | |
} | |
return instance; | |
} | |
/** | |
* Returns instance of Resource. | |
* | |
* New resource will be created if all the resources | |
* were used at the time of the request. | |
* | |
* @return Resource instance. | |
*/ | |
Resource* getResource() | |
{ | |
if (resources.empty()) | |
{ | |
std::cout << "Creating new." << std::endl; | |
return new Resource; | |
} | |
else | |
{ | |
std::cout << "Reusing existing." << std::endl; | |
Resource* resource = resources.front(); | |
resources.pop_front(); | |
return resource; | |
} | |
} | |
/** | |
* Return resource back to the pool. | |
* | |
* The resource must be initialized back to | |
* the default settings before someone else | |
* attempts to use it. | |
* | |
* @param object Resource instance. | |
* @return void | |
*/ | |
void returnResource(Resource* object) | |
{ | |
object->reset(); | |
resources.push_back(object); | |
} | |
}; | |
ObjectPool* ObjectPool::instance = 0; | |
int main() | |
{ | |
ObjectPool* pool = ObjectPool::getInstance(); | |
Resource* one; | |
Resource* two; | |
/* Resources will be created. */ | |
one = pool->getResource(); | |
one->setValue(10); | |
std::cout << "one = " << one->getValue() << " [" << one << "]" << std::endl; | |
two = pool->getResource(); | |
two->setValue(20); | |
std::cout << "two = " << two->getValue() << " [" << two << "]" << std::endl; | |
pool->returnResource(one); | |
pool->returnResource(two); | |
/* Resources will be reused. | |
* Notice that the value of both resources were reset back to zero. | |
*/ | |
one = pool->getResource(); | |
std::cout << "one = " << one->getValue() << " [" << one << "]" << std::endl; | |
two = pool->getResource(); | |
std::cout << "two = " << two->getValue() << " [" << two << "]" << std::endl; | |
return 0; | |
} |
Adding below code in Object Pool destructor would help resolve memory leak issue:
while(resources.size())
{
Resource *obj = resources.front();
resources.pop_front();
delete obj;
}
Isn't it a problem when someone keeps call getresource but never return resource? I suppose resource pool size should be defined when pool created or limited to expand.
OUTPUT??
output is:
one = 10 [0x1004001e0]
two = 20 [0x100407210]
one = 0 [0x1004001e0]
two = 0 [0x100407210]
Program ended with exit code: 0
How would you construct a mechanism if your pool of unused resources grows in size, thereby increasing the memory footprint, but the resources are not being used for a long time.
(Note: This is not memory leak)
made with singleton desing pattern. Good Job !
Leaving this here for reference: https://stackoverflow.com/questions/27827923/c-object-pool-that-provides-items-as-smart-pointers-that-are-returned-to-pool
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
resources are not deleted, causes memory leak.