Does ArrayResize allocate additional objects using the default constructor?

 

I get a memory leak when working on my new indicator. 

At the beginning I allocate an array of my Object which later gets resized and filled. Apparently there are exactly as many objects leaking as the resize operation. How do I clean up and/or initialize my array correctly?


MyObject instance[];


int OnInit(){

  ArrayResize(instance,newSize);

  for(int i = 0; i < newSize; i++){
    instance[i] = new MyObject(par1,par2);
  }
}

void OnDeinit(const int reason){
  
  var size = ArraySize(instance);
  for(int i = size -1 ; i>=0; i--){
   delete(&instance[i]);
  } 
  //ArrayFree(instance);
}

 
kilian13: I get a memory leak when working on my new indicator. At the beginning I allocate an array of my Object which later gets resized and filled. Apparently there are exactly as many objects leaking as the resize operation. How do I clean up and/or initialize my array correctly?
By freeing up the array - ArrayFree()
 
Fernando Carreiro:
By freeing up the array - ArrayFree()

Thank you for your comment, but I could not successfully fix my code. According to the documentation arrayFree sets the size of the array to 0 which is not what I want since I need the array to be of size n. If I use ArrayFree() in the indicator deinit function I get the same memory leak.  I assume that ArrayResize() creates default instances of the object which only get overwritten by me later on. 

 
kilian13: Thank you for your comment, but I could not successfully fix my code. According to the documentation arrayFree sets the size of the array to 0 which is not what I want since I need the array to be of size n. If I use ArrayFree() in the indicator deinit function I get the same memory leak.  I assume that ArrayResize() creates default instances of the object which only get overwritten by me later on. 
MyObject  instance[]; // This is what you have and it pre-allocates the objects when you resize it, because it stores objects and not pointers.
MyObject *instance[]; // This is probably what you meant to do, as the array now only stores pointers to the objects and not the objects themselves.

So you have two options:

Option 1:

MyObject *instance[]; // Array of pointers to objects

int OnInit(){

  ArrayResize(instance,newSize);

  for(int i = 0; i < newSize; i++){
    instance[i] = new MyObject(par1,par2);
  }
}

void OnDeinit(const int reason){
  
  var size = ArraySize(instance);
  for(int i = size -1 ; i>0; i--){
   delete(instance[i]); // Don't use the "&" as it is already a pointer
  } 
}

Option 2:

MyObject instance[]; // Array of Objects

int OnInit(){

  ArrayResize(instance,newSize); // All objects are per-allocated and constructed

  for(int i = 0; i < newSize; i++){
    instance[i].SetParam(par1,par2); // Setting Parameters via a class Method instead of via constructor
  }
}

void OnDeinit(const int reason){
  
  // Nothing to do as array will be freed automatically
}
 
  1. MyObject instance[];
    instance[i] = new MyObject(par1,par2);
    Instance is an array of objects, not an array of pointers. Thus, that copies the newed object to the array element. It does not store the pointer there. The newed object pointed is then lost, and that is your memory leak.

  2. for(int i = size -1 ; i>0; i--)   delete(&instance[i]);
    This does nothing, because the pointers passed to delete are not dynamic.

  3. Using ArrayFree is unnecessary, the objects will be deconstructed when global variables are.
Reason: