Advertisement
RobertDeMilo

BB3.10-11 Обнаружение утечки памяти в ObjectPool и откуда берется утечка памяти

Jun 13th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. main
  2.  
  3. #include "ObjectPool.h"
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int counter = 0;
  9. struct Counted
  10. {
  11.     Counted()
  12.     {
  13.         ++counter;
  14.     }
  15.     ~Counted()
  16.     {
  17.         --counter;
  18.     }
  19. };
  20.  
  21. void run()
  22. {
  23.     //ObjectPool<char> pool;
  24.     ObjectPool<Counted> pool;
  25.  
  26.     cout << "Counter before loop = " << counter << endl;
  27.  
  28.     try
  29.     {
  30.         for (int i = 0; i < 1000; ++i)
  31.         {
  32.             cout << "Allocating object #" << i << endl;
  33.             pool.Allocate();
  34.         }
  35.     }
  36.     catch (const bad_alloc& e)
  37.     {
  38.         cout << e.what() << endl;
  39.     }
  40.  
  41.  
  42.     cout << "Counter after loop = " << counter << endl;
  43. }
  44.  
  45. int main()
  46. {
  47.     run();
  48.     cout << "Counter before exit = " << counter << endl;
  49.  
  50.     return 0;
  51. }
  52.  
  53. ////////////////////////////////////////////////////////////////////
  54. ObjectPool.h
  55.  
  56. #pragma once
  57. #include<algorithm>
  58. #include<string>
  59. #include<queue>
  60. #include<stdexcept>
  61. #include<set>
  62.  
  63. using namespace std;
  64.  
  65. template <class T>
  66. class ObjectPool
  67. {
  68. public:
  69.  
  70.     T* Allocate();
  71.     T* TryAllocate();
  72.  
  73.     void Deallocate(T* object);
  74.     ~ObjectPool();
  75.    
  76. private:
  77.  
  78.     queue<T*> free;
  79.     set<T*> allocated;
  80.  
  81. };
  82.  
  83.  
  84. template <typename T>
  85. T* ObjectPool <T>::Allocate()
  86. {
  87.     if (free.empty())
  88.     {
  89.         free.push(new T);
  90.     }
  91.     auto ret = free.front();
  92.     free.pop();
  93.     try
  94.     {
  95.         allocated.insert(ret);
  96.     }
  97.     catch (const bad_alloc&)
  98.     {
  99.         delete ret;
  100.         throw;// проброс исключения дальше
  101.     }
  102.     return ret;
  103. }
  104.  
  105. template <typename T>
  106. T* ObjectPool <T>::TryAllocate()
  107. {
  108.     if (free.empty())
  109.     {
  110.         return nullptr;
  111.     }
  112.     return Allocate();
  113. }
  114.  
  115. template <typename T>
  116. void ObjectPool <T>::Deallocate(T* object)
  117. {
  118.     if (allocated.find(object) == allocated.end())
  119.     {
  120.  
  121.         throw invalid_argument(" ");
  122.     }
  123.     allocated.erase(object);
  124.     free.push(object);
  125. }
  126.  
  127. template <typename T>
  128. ObjectPool<T>::~ObjectPool()
  129. {
  130.     for (auto x : allocated)
  131.     {
  132.         delete x;
  133.     }
  134.     while (!free.empty())
  135.     {
  136.         auto x = free.front();
  137.         free.pop();
  138.         delete x;
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement