0

I am attaching the minimal code below. The problem is with static string object that is leaking memory. I think the problem is with the string object not being initialized properly. The program runs fine in Debug mode but crashes in the Release mode.

I am using Windows 7 : 64bit - MS Visual Studio 2012

I have tried initializing the object with empty string but it did not solve the problem as suggested here what to do if debug runs fine, but release crashes

I enabled "Treating warnings as Errors" also did not help as there are no warning as suggested by the following post what to do if debug runs fine, but release crashes

There were some other suggestions too like "static initialization order fiasco" but I do not think its related to my issue.

Any help is appreciated

main.cpp

//main.cpp
#include "MyParameters.h"

using namespace std ;

int main( int argc, char *argv[] )
{
  try
  { 
        cout << "MyParameters::m_outputDir: " << MyParameters::m_outputDir << endl ;

        bool initialized = MyParameters::initialize( "myimo.xml" ) ;                

        cout << "MyParameters::m_outputDir: " << MyParameters::m_outputDir << endl ;
        cout << "Terminating the application..." << endl ;

  }
  catch ( std::exception &e )
  {
    cout << e.what() << std::endl;
  }
}

MyParameters.h

//MyParameters.h
#ifndef __MY_PARAMETERS_H
#define __MY_PARAMETERS_H

#include <string>
#include <iostream>

 #include <QString>

class MyParameters 
{

public:

  static std::string m_outputDir; ///< output directory

  static bool initialize( const QString &xmlFile  );

private:

  MyParameters();
};

#endif /* __MY_PARAMETERS_H */

MyParameters.cpp

//MyParameters.cpp
#include "MyParameters.h"
#include <QDir>

std::string MyParameters::m_outputDir ;    

using namespace std ;

MyParameters::MyParameters()
{

}

bool MyParameters::initialize( const QString &xmlFile )
{
  m_outputDir = QDir::current().absoluteFilePath( xmlFile ).toStdString(); // --> this crashes 
    //m_outputDir = "C:\\Dev\\" ; // --> works fine
    cout << "m_outputDir: " << m_outputDir << endl ;

    cout << "myparameters.xml file reading is complete" << endl ;
  return true;
}
Community
  • 1
  • 1
Schnell
  • 1
  • 2
  • 4
    [MVCE](http://stackoverflow.com/help/mcve) please. – Borgleader Oct 23 '14 at 12:43
  • What is the output of your program, before it crashs...? – jpo38 Oct 23 '14 at 12:44
  • @jpo38: It prints on console **"Terminating the application..."** the last line of the main and then crashes. – Schnell Oct 23 '14 at 12:47
  • Same if you add return 0 at the end of your main function? Was Qt compiled correctly? Try a simple Qt program only creating a static std::string. – jpo38 Oct 23 '14 at 13:07
  • There's a lot you could and should trim to narrow down the problem. For example, what if you alter `ParameterFileParser::checkDirectory` so that it just returns a copy of the input string? What if `setDirectory` just sets the `value` to a fixed string and returns `true`? Take @Borgleader's advice and create an MVCE. You may solve your own problem on the way. – Edward Oct 23 '14 at 13:09
  • 2
    TMTR but you're probably up against the [static initialization order fiasco](http://www.parashift.com/c++-faq/static-init-order.html). It's nearly always a good idea to consult the FAQ before asking. – Cheers and hth. - Alf Oct 23 '14 at 13:14
  • thanks guys for the suggestions. I ll try them now. – Schnell Oct 23 '14 at 13:20
  • i have reduced the example to minimal. The program crashes at `QDir::current().absoluteFilePath( xmlFile ).toStdString();` but if i use a fixed string then it works fine. The crash occurs only in the release mode, debug mode runs fine. – Schnell Oct 23 '14 at 15:21
  • Nice and now split that line up into 3 different lines and see which fails. – Surt Oct 23 '14 at 16:51
  • I have broken it down to 3 separate lines but the program crashes when the main exits. It does not crash at `QDir::current().absoluteFilePath( xmlFile ).toStdString();`. When I am using a fixed string instead of `QString.toStdString()` then the program runs fine in release mode. Also it runs fine in the Debug mode but crashes only in the release mode. I can not figure out why it is crashing when main exits. Thanks a lot for all the suggestions from you guys. – Schnell Oct 24 '14 at 10:56
  • ok, i think i have found the problem. The program was not crashing in the ReleaseWithDebugInfo. But I tried again in ReleaseWithDebugInfo, compiled the program and enabled the flag Properties->Linker->Debugging->Generate Debug Info (Yes /DEBUG). Then when I executed the program it crashed and I clicked the DEBUG button. the CallStack showed me **dynamic atexit destructor for `MyParameters::m_outputDir`**. Now how to solve this issue is another question which I will try to figure out but I have no idea what **dynamic atexit** is. Thanks a lot for the help. – Schnell Oct 24 '14 at 12:31

0 Answers0