C++ c++;静态字符串对象在释放模式下崩溃
我附上下面的最小代码。问题在于静态字符串对象正在泄漏内存。我认为问题在于字符串对象没有正确初始化。程序在调试模式下运行良好,但在发布模式下崩溃 我正在使用Windows 7:64位-MS Visual Studio 2012 我尝试过用空字符串初始化对象,但它并没有像这里建议的那样解决问题 我启用了“将警告视为错误”也没有帮助,因为没有以下帖子建议的警告 还有其他一些建议,比如“静态初始化命令失败”,但我认为这与我的问题无关 谢谢你的帮助 main.cppC++ c++;静态字符串对象在释放模式下崩溃,c++,static,stdstring,C++,Static,Stdstring,我附上下面的最小代码。问题在于静态字符串对象正在泄漏内存。我认为问题在于字符串对象没有正确初始化。程序在调试模式下运行良好,但在发布模式下崩溃 我正在使用Windows 7:64位-MS Visual Studio 2012 我尝试过用空字符串初始化对象,但它并没有像这里建议的那样解决问题 我启用了“将警告视为错误”也没有帮助,因为没有以下帖子建议的警告 还有其他一些建议,比如“静态初始化命令失败”,但我认为这与我的问题无关 谢谢你的帮助 main.cpp //main.cpp #包括“My
//main.cpp
#包括“MyParameters.h”
使用名称空间std;
int main(int argc,char*argv[])
{
尝试
{
请问。在程序崩溃之前,您的程序的输出是什么?@jpo38:它在控制台上打印“终止应用程序…”main的最后一行然后崩溃。如果在main函数的末尾添加return 0也是一样的?Qt编译是否正确?尝试一个简单的Qt程序,只创建一个静态std::string.TMTR,但您可能遇到了问题。在询问之前,最好先查阅常见问题解答。
//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
#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
#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;
}