title: crash when in release mode when writing to a global array defined in main.cpp from a dialog
body: <p>I am having a strange issue with a C++/QT programming I am writing</p>
  
  <p>In my program I have two UIs  one for the main window and one for a dialog that is popped up when a action is selected from a menu off a qtreeview.  The dialog is supposed to be used to add more things to the tree.</p>
  
  <p>I  have a global class that holds a large char array.  This is supposed to hold all the data I want to save. I am using a malloc to allocate the data.   I have no trouble writing to this array when in my mainwindow class. However when I try to write to the array when in the dialog class and I am in release the program sometimes (about 30 % of the time) dies. It works perfectly in debug mode and the array data in debug mode looks correct. All the pointers look to be pointing to the correct spots in memory when in debug mode.</p>
  
  <p>Does anyone have any idea what might be going wrong.</p>
  
  <p>The main.cpp</p>
  
  <pre><code>GameMemoryMan GmemMan;
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      GMemMan.setBaseMemorySize((int64_t)1000000000);
      MainWindow w;
      w.init();
      w.show();
      return a.exec();
  }
  </code></pre>
  
  <p>In the .h  for the GameMemoryMan</p>
  
  <pre><code>#define TO_PTR(Type, Index, MeM) Index == 0 ? nullptr : (Type*)(&amp;MeM.GameData[Index])
  #define TO_INDEX(PTR, MeM)  PTR == nullptr ? 0 : ((unsigned char*) PTR - (MeM.GameData));
  #define GET_PTR(Type, MeM) (Type*)(&amp;MeM.WritePoint[0]);MeM.WritePoint = MeM.WritePoint + sizeof(Type);
  #define GET_TYPE(PTR) (*(int64_t*)(PTR))
  class GameMemoryMan
  {
  public:
      GameMemoryMan(){}
      ~GameMemoryMan(){}
      int setBaseMemorySize(int64_t );
      int LoadGameMemory(std::string GameLoction);
      int SaveGameLocation(std::string GameLocation);
      int64_t WriteMem(void*, int64_t );
      int64_t GetIndex(void* pointer);
      std::string ReadString(int64_t index);
      int64_t WriteString(std::string TheString);
      void CheckMemorySize();//check if more data is needed. Must be done regulerly;
  
      unsigned char *GameData;
      unsigned char *WritePoint= nullptr;
      int64_t BaseMemSize = 0;
  private:
  };
  
  //DataBlock: 002
  struct location_node
  {
      int64_t Type = 2;
      int64_t name_inx; //string
      int64_t Parent_inx; //002 or 001 if Top
      int64_t child_inx; // 002 or 003
      int64_t siblings_inx; // 002 or 003
      int64_t packing[16];
  };
  extern GameMemoryMan GMemMan;
  </code></pre>
  
  <p>In the cpp for the GameMemoryMan</p>
  
  <pre><code>int GameMemoryMan::setBaseMemorySize(int64_t Size)
  {
      GameData = (unsigned char*) malloc(Size*sizeof(unsigned char));
      BaseMemSize = Size;
      memset(GameData, 0, Size);
      //((MainGame*)(GameData))-&gt;BaseMemSize = BaseMemSize;
      //MainGame* pointer = TO_PTR(MainGame, 0, this);//(MainGame*)(&amp;GameData[0]);
      MainGame* pointer = (MainGame*)(&amp;this-&gt;GameData[0]);
      pointer-&gt;BaseMemSize = BaseMemSize;
      //int64_t* pointer = (int64_t*)(&amp;GameData[0]);
      //pointer = BaseMemSize;
      return 0;
  }
  </code></pre>
  
  <p>//This creates the Dialog</p>
  
  <pre><code>void MainWindow::AddLocation()
  {
       AddNewLocation NewLocationDialog;
       NewLocationDialog.setModal(true);
       NewLocationDialog.exec();
      return;
  }
  </code></pre>
  
  <p>Dies when this is run</p>
  
  <pre><code>void AddNewLocation::AddLocation()
  {
      std::string nameOfLocation = ui-&gt;lineEdit-&gt;text().toStdString();
      if(treemodel-&gt;findItems(nameOfLocation.c_str(), Qt::MatchContains | Qt::MatchRecursive, 0).isEmpty())
      {
  
          RoomNode* item0 = new RoomNode(folder, nameOfLocation.c_str());
          location_node* newlocation = GET_PTR(location_node, GMemMan);
  
          item0-&gt;savedata = newlocation;
  
          newlocation-&gt;Type = 2; //code sometimes dies hear
  
          newlocation-&gt;name_inx = GmemMan.WriteString(nameOfLocation); // or hear
  
  
  
          treeNodeParent-&gt;appendRow(item0);
  
  
      return;
  }
  </code></pre>