Invalid Pointer Access

 
Good day. 

I have a code panel now this panel works fine but when I change chart or Time frame I get an error (Invalid Pointer Access ) 

      MyButton *b = (MyButton*)m_buttons.At(i);
      b.Color(clrWhite);                                  //font color
      b.ColorBackground(GetButtonColor(b.Text()));      //button color
this is my Class 

#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>

//+------------------------------------------------------------------+
//|Classes for the risk management tool                              |
//+------------------------------------------------------------------+
//--button class from CButton class with event processing function
class MyButton: public CButton
  {
private:
   TAction           m_action;                  //chart events handler
public:
                     MyButton(void) {}          //default constructor
                    ~MyButton(void) {}          //default destructor
   //--- constr specifying button text and the pointer to the events handling function
                     MyButton(string text, TAction act)
     {
      Text(text);
      m_action = act;
     }
   //--- custom funct called from OnEvent() events handler
   void              SetAction(TAction act) {m_action = act;}
   //--- standard chart events handler
   virtual bool      OnEvent(const int id, const long &lparam,
                             const double &dparam, const string &sparam) override
     {
      if(m_action != NULL && lparam == Id())
        {
         //--custom m_action() handler call
         m_action(sparam, (int)lparam);
         return(true);
        }
      else
        {
         //--return the result of calling event handler from CButton parent class
         return(CButton::OnEvent(id, lparam, dparam, sparam));
        }
     }
  };
 

Not easy to say what the problem is without the full code or a reproducible example. This error occurs when you try to access an object from a NULL pointer. Take this example:

CExampleClass* cls;
cls.DoSomething();


In this case, cls is not initialized and contains a NULL pointer. Trying to access the object will just fail. The correct way to do it would be like this:


CExampleClass* cls; // assuming this is a global variable

int OnInit()
{
cls = new CExampleClass;

cls.DoSomething();
return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
        delete cls;
}


Considering this, I'll assume the error lays in this line:


 MyButton *b = (MyButton*)m_buttons.At(i);


If m_buttons is an array of buttons, check if the CButton at the index i is a NULL pointer (m_buttons.At(i) == NULL) and initialize it properly. Also, always remember to delete every object initialized with new (every element of arrays that contains pointers should be deleted) to avoid memory leaks.


If the error is not on the mentioned line, please provide more information (like a bit more of the code, the line where the error occurs, etc.).

 
Jefferson Judge Metha:
Good day. 

I have a code panel now this panel works fine but when I change chart or Time frame I get an error (Invalid Pointer Access ) 

this is my Class 

Look at the line where the error is located and send us the whole code eg. everything inside OnInit or DeInit etc wherever the problem is....This snippet has an error at cls initialization, but there may be more errors because of this and sometimes fixing one problem, causes others. If you experience any, send the source code or part of the code so we can take a look but its hard to work off a small snippet like this as we do not know what the rest looks like

Reason: