C++ Member variable losing value after constructor

I'm having a problem in one of my classes. I have these 2 classes, GameApp and Simulation:


#pragma once

#include "Simulation.h"

class Simulation;

class GameApp {

    static GameApp *instance;



    static GameApp *initializeContext(const char *gameTitle, const int windowWidth, const int windowHeight);

    //void setSimulation(Simulation &simulation) { *(this->simulation) = simulation; }
    Simulation *getSimulation() { return simulation; }

    static const int TARGET_FPS = 50; // Frames per second


    Simulation *simulation;

    double frameIntervalList[TARGET_FPS]; // A list containing the time to render the last 60 frames
    // Other stuff that doesn't matter


#pragma once

#include <vector>
#include "GameApp.h"

class Simulation {
    Simulation(const Simulation &copy);

    Simulation &operator=(const Simulation &other);

    std::vector<Entity*> *entities;

And, in GameApp.cpp, in the function initializeContext, I have:

#include "GameApp.h"

GameApp *GameApp::instance = NULL;

GameApp::GameApp() {
    gamePaused = false;
    frameIntervalSum = 0;
    this->simulation = new Simulation();
    for (int i = 0; i < 60; i++) {
        frameIntervalList[i] = 0;

GameApp *GameApp::initializeContext(const char *gameTitle, const int windowWidth, const int windowHeight) {
    instance = new GameApp();

    // (...) Rest of initialize funcion

The problem is, for some misterious reason, when I call initializeContext, the first line of it calls GameApp's constructor, which creates a new Simulation, which allocates correctly and I get a memory address for the simulation pointer. But when the program exits the constructor of GameApp, in the line right after instance = new GameApp();, if I check the simulator variable on the newly created instance using the debugger, I get the pointer value of 0x00000000, and the Simulation that I just created is gone. This would indeed happen if I was using the stack memory on the constructor, but I'm clearly using new, creating the new Simulation variable in the correct way, I guess. What may be happening here?

Also, there's a commented setSimulation function on the GameApp.h file. When I leave this uncommented, I get the compiler error 2582, "operator= function is unavailable in Simulation". Can this be related to my problem?

EDIT: The problem was indeed the for loop with the hardcoded size. I changed the size of frameIntervalList from 60 to 50 in the header but forgot to change it on the loop. Also updated code to show the declaration of frameIntervalList.


The only code that executes between the initialization of the simulation member variable and the code that comes after the GameApp ctor is the for loop that initialzes frameIntervalList so its initialization with a hardcoded size might overwrite the value of simulation variable if you are unlucky. (Or lucky because it helps you to catch a very ugly bug early!!! :-)

Need Your Help

Android SDK Install Packages Error

android sdk install packages proxy-server

I've been digging for hours over this, can someone please break it down in to basic newbie military grunt (what I am, currently deployed) terms cause I promise I'm not the only one that will benefi...

How do modify the look and feel of a Sharepoint site?

css sharepoint look-and-feel branding

I somehow got lumbered with deploying SharePoint at work. I hate it, even the simplest things seem to be unnecessarily difficult.