1.发展环境
1)WIN7 64bit
2)VS2013
3)QT-开源窗口86 msvc2013_opengl-5.4.0
4)QT-VS-插件-1,2,4-开源
PS:vs2013使用多线程DLL调试(/ MDd)
2. testQT项目包括3个文件,Form.cpp,testString.h和testString.cpp
1)Form.cpp
//......
#include "testString.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
//...
void (QObject::*p)(void) = (void (QObject::*)(void))&Form::updateUI;
QString sss("aa");
TestString::Instance()->printClassComFunc(sss, this, p, sss);
}
2)testString.h
#ifndef TestString_H
#define TestString_H
#include <string>
#include <map>
#include <vector>
#include <QString>
class QWidget;
class QObject;
using namespace std;
typedef void (QObject::*NotifySelector1)();
#define notify_selector1(selector) (NotifySelector1)(&selector)
class TestString;
struct NotifyItem1
{
QObject* observer;
void (QObject::*selector)();
string selectorName;
};
class TestString
{
private:
TestString();
static TestString* _instance;
map<string, vector<NotifyItem1>* > notifymap;
public:
static TestString* Instance();
virtual ~TestString();
void printClassComFunc(QString name1, QObject* observer, void(QObject::*func)(), QString name);
};
#endif // TestString_H
3)testString.cpp
//...
void TestString::printClassComFunc(QString name1, QObject* observer, void(QObject::*func)(), QString name)
{
printf("printClassComFunc name=%s\n", name.toStdString().c_str());
return;
}
在函数printClassComFunc中,param name1是“aa”,但名称是“????”和记忆崩溃。
当我从
注释成员函数指针时struct NotifyItem1
{
QObject* observer;
void (QObject::*selector)();
string selectorName;
};
到
struct NotifyItem1
{
QObject* observer;
//void (QObject::*selector)();
string selectorName;
};
问题没有发生,但它是随机的,我不认为这是重点。
4.我的分析
1)问题的背景
我将我的其他项目从mac(10.9.0)传输到窗口win7(64位),std:字符串内存崩溃,堆免费,我想也许它是std库的不同实现,windows使用xstring和mac使用字符串,所以我将std:string替换为QString,问题也存在。
2)我谷歌很多,有人说你可能加载了许多不同的c运行时库, 我使用依赖walker并发现我只使用msvcr120d.dll和msvcp120d.dll,qt内置qt5cored.dll的old32.dll使用msvcrt.dll,但我不认为这是关键问题。
3)其他一些人说你可能在exe应用程序上分配内存并在dll端释放它。 链接是
QString::toStdString() crashes on std::string destructor
https://support.microsoft.com/en-us/kb/813810
我认为这主要是关键点,但我也无法解决我的问题。
所以你的帮助是我的赞赏。