Invalid pointer access using copy constructor?

 

Hi there,

Some very simple code below to illustrate a problem I'm having. I have a very simple class with a copy constructor. The sample below creates a new object and then tries to make a copy of that object via the copy constructor whereupon I get the invalid pointer access error/message.

What am I doing wrong here?

Many thanks.


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CSnrRange
  {
public:
   datetime          EarliestTime;
   datetime          HighTime;
   double            HighVal;
   datetime          LowTime;
   double            LowVal;
   bool              Ignore;
public:
                     CSnrRange() {};
                    ~CSnrRange() {};
                     CSnrRange(const CSnrRange &range); // Copy
   bool              IsMatch(CSnrRange* target);
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CSnrRange::CSnrRange(const CSnrRange &range)
  {
   EarliestTime = range.EarliestTime;
   HighTime = range.HighTime;
   HighVal = range.HighVal;
   LowTime = range.LowTime;
   LowVal = range.LowVal;
   Ignore = range.Ignore;
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   CSnrRange* firstRange = new CSnrRange();
   CSnrRange* copy = CSnrRange(firstRange); // 2021.04.26 12:47:38.679  Test GBPUSD,Daily: invalid pointer access in 'Test.mq4' (56,15)

   DebugBreak();

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
  1.    CSnrRange* copy = CSnrRange(firstRange); // 2021.04.26 12:47:38.679  Test GBPUSD,Daily: invalid pointer access in 'Test.mq4' (56,15)
    

    You call the copy constructor the result is an unnamed object. You then try to assign the object to a pointer.

  2. Just copy the object.

     CSnrRange copy(firstRange);

  3. CSnrRange::CSnrRange(const CSnrRange &range)
      {
       EarliestTime = range.EarliestTime;
       HighTime = range.HighTime;
       HighVal = range.HighVal;
       LowTime = range.LowTime;
       LowVal = range.LowVal;
       Ignore = range.Ignore;
      }
    I almost always name the right hand side that, and construct the object, not assign to it after construction:
    CSnrRange::CSnrRange(const CSnrRange &that)
     : EarliestTime(that.EarliestTime),
       HighTime(that.HighTime),
       HighVal(that.HighVal),
       LowTime(that.LowTime),
       LowVal(that.LowVal),
       Ignore(that.Ignore)
      {}

 
William Roeder:
  1. You call the copy constructor the result is an unnamed object. You then try to assign the object to a pointer.

  2. Just copy the object.


  3. I almost always name the right hand side that, and construct the object, not assign to it after construction:

Thank you very much for pointing this out! I appreciate it :)