0

I am using C++ 98. When I try to run the below code and note the task manager - the memory consumption goes very high. Can someone explain if there is a leak in the commented part in main , if so how could this be fixed?

#include <iostream>
#include<vector>
using namespace std;

template<typename T> class ArrayHandler
{
    public:
    
    ArrayHandler()
    {
        val=NULL;
        count=0;
    }
    
    ~ArrayHandler()
    {
        freecontent();
    }
    
    void assign(T* p, int32_t icount)
    {
        freecontent();
        count=icount;
        val= new T[count];
        for(int32_t i=0;i<icount;i++)
        {
            val[i]=p[i];
            
        }
    }
    
    void resize(int32_t newsize)
    {
        if(newsize==count)
        return ;
        else
        {
            T* p= new T[newsize];
            int32_t maxcount= count-1;
            
            for(int32_t i=0; i< maxcount;i++)
            {
               if(maxcount>i)
                 p[i]= val[i];
               else
                 p[i]= val[maxcount];
                 
            }
            assign(p, newsize);
            }
        }
        
  private:
  
   T* val;
   int32_t count;
   
   void freecontent()
   {
       delete[] val;
       val=NULL;
   }
};



template<typename T> void handlesettingsize(T* p, ArrayHandler<T>& val, int32_t icount)
{
    
     val.assign(p,icount);
    if(icount==4)
    {
       //do nothing
    }
    else
    {
        val.resize(100);
    }
    
}

int main()
{
  ArrayHandler<double> val;
  for(int32_t i=0;i<15000;i++)
  {
   double *p =new double[6];
    for(int32_t i=0;i<6;i++)
     p[i]=0.0+i;
   
   handlesettingsize(p, val, 5); // Task manager shows increased memory consumption if not commented
   delete[] p;
    p=NULL;
  }

    return 0;
}
  • 2
    use valgrind rather than your taskmanager. As memory is not immediately handed back to the OS, thats rather unreliable to detect leaks. I didn't spot the leak but a different problem. You `delete[] p;` but `p` was not allocated via `new []`, thats certainly wrong – 463035818_is_not_an_ai Nov 10 '20 at 13:10
  • 5
    `delete[] p;` has undefined behavior. I'm surprised the program didn't crash. `p` does not point to memory created by `new[]` so you can't `delete[]` it. – NathanOliver Nov 10 '20 at 13:12
  • what is the code supposed to achieve? I am certain that also in C++98 there was no reason to do such acrobatics with `new` and `delete`. If you want a resizable array, thats `std::vector`, available since the very beginning – 463035818_is_not_an_ai Nov 10 '20 at 13:14
  • if you do such stuff you must read about the [rule of 3](https://en.cppreference.com/w/cpp/language/rule_of_three) and follow it. If you don't, you will run into all kinds of serious problems, not just leaks – 463035818_is_not_an_ai Nov 10 '20 at 13:16
  • Not a bug as such, but when your loop condition is `i< maxcount`, how likely is it that `maxcount>i` is false inside it? (You're not copying the final element. It looks like you have been trying to fix a bug by adjusting the indexing.) – molbdnilo Nov 10 '20 at 13:17
  • @NathanOliver I have edited the code. The main point is, does the template ```ArrayHandler``` carry leaks. – G.Dinesh Nathan Nov 10 '20 at 13:25
  • @idclev463035818 , I agree on vectors. But the point is, these functionalities are in a library in my project. – G.Dinesh Nathan Nov 10 '20 at 13:26
  • 2
    @G.DineshNathan Yes, it might contain leaks. See [this](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) for why. – NathanOliver Nov 10 '20 at 13:27
  • @molbdnilo This was just an example that the condition won't fail. I just made a minimal usable code to depict my problem. its quite likely that might fail each time. – G.Dinesh Nathan Nov 10 '20 at 13:27
  • 1
    the best answer to your question really is: Use valgrind. Maybe you are lucky and someone can spot the leak in your code, but the only realiable way is to use an analysis tool. Apart from that `ArrayHandler` is definitely broken, because violation of rule of 3, its just not happening in your code, but UB is lurking around the next corner. And as `delete[] p;` definitely is UB, the question if there are leaks is kinda secondary, your code is not valid C++ (whether it has leaks or not) – 463035818_is_not_an_ai Nov 10 '20 at 13:32
  • @idclev463035818 I will check on valgrind. On the contrary, if you assume apart from the given code, a copy constructor is present as a class member, does the resize still carry potential leaks ? – G.Dinesh Nathan Nov 10 '20 at 13:36
  • I dont understand what you mean. Your code has undefined behavior. If you compile and run it anything can happen. And the reason for that is not in `ArrayHandler` itself. `ArrayHandler` has more problems but the most striking problem is in `main`, no change in `ArrayHandler` will fix that. – 463035818_is_not_an_ai Nov 10 '20 at 13:39
  • and please do read about the rule of 3 (links above). If you think adding a copy constructor is sufficient then you didn't understand the rule of 3. – 463035818_is_not_an_ai Nov 10 '20 at 13:40
  • @idclev463035818 Maybe I mis understood rule of 3. I will give a read carefully. Thanks for the suggestion. – G.Dinesh Nathan Nov 10 '20 at 13:41

0 Answers0