title: Qt C++: Global objects vs. reference chain
body: <p>Currently I'm using singleton pattern for certain global objects in my application (Qt application for Symbian environment). However, because of some problems (<a href="https://stackoverflow.com/questions/3147375/c-checking-singleton-pointers/3147806#3147806">C++ checking singleton pointers</a>) it looks like that I have to change the logic.</p>
  
  <p>I have 3 classes (logger, settings and container for some temp data) that I need to access via multiple different objects. Currently they are all created using singleton pattern. The logger basically is just one public method Log() with some internal logic when the settings and container have multiple get/set methods with some additional logic (e.g. QFileSystemWatcher). In addition, logger and settings have some cross-reference (e.g. logger needs some settings and settings logs errors).</p>
  
  <p>Currently everything is "working fine", but there is still some problems that should be taken care of and it seems like they are not easy to implement for the singletons (possible memory leaks/null pointers). Now I have two different ways to handle this:</p>
  
  <ol>
  <li>Create global objects (e.g. extern Logger log;) and initialize them on application startup. </li>
  <li>Create objects in my main object and pass them to the children as a reference.</li>
  </ol>
  
  <p>How I have few questions related to these:</p>
  
  <p><strong>Case 1.</strong> </p>
  
  <ul>
  <li>Is it better to use stack or heap?</li>
  <li>I'm going to declare those objects in some globals.h header using extern keyword. Is it ok?</li>
  <li>I think in this case I have to remove that 2-way reference (settings needs logger and vice versa.)?</li>
  </ul>
  
  <p><strong>Case 2.</strong></p>
  
  <ul>
  <li><p>Should the objects be created in stack or heap in my main object (e.g. Logger *log = new Logger() vs Logger log;)</p></li>
  <li><p>Long reference chains do not look nice (e.g. if i have to pass the object over multiple childrens).</p></li>
  <li><p>What about in children? </p>
  
  <ol>
  <li>If I pass a pointer to the children like this (I don't want to copy it, just use the "reference"): Children(Logger *log) : m_Log(log) what happens when the children is deleted? Should I set the local pointer m_Log to NULL or? </li>
  <li>If I use stack I'll send reference to the child (Children(Logger &amp;log) : m_Log(log)) where m_Log is a reference variable (Logger&amp; m_Log;) right?</li>
  </ol></li>
  <li><p>What should I note in terms of Qt memory management in this case?</p></li>
  </ul>
  
  <p><strong>Case 3.</strong> 
  Continue with singleton and initialize singleton objects during the startup (that would solve the null pointers). Then the only problem would possible memory leaks. My implementation follows <a href="http://www.yolinux.com/TUTORIALS/C++Singleton.html" rel="nofollow noreferrer">this</a> example. Is there a possible memory leak when I'm accessing the class using. What about singleton destruction?
      #define LOG Logger::Instance()->Log</p>
  
  <p>Thanks for reading.</p>