设置m_pszAppName值的做法

作者:朱金灿

来源:http://blog.csdn.net/clever101

 

         CWinApp::m_pszAppName用于指定应用程序的名字。昨天这样修改它的值:

 

m_pszAppName = m_AppInfo.m_strAppName.c_str();

     其中m_AppInfo.m_strAppName是一个std::string

     结果在程序退出时出现崩溃:

         原来m_pszAppName在程序退出时会被释放的,而m_AppInfo.m_strAppName也会执行析构函数,因此二者是有冲突的。后来查了下MSDN,发现设置m_pszAppName值的做法是这样的:

//First free the string allocated by MFC at CWinApp startup.
//The string is allocated before InitInstance is called.
free((void*)m_pszAppName);
//Change the name of the application file.
//The CWinApp destructor will free the memory.
m_pszAppName = _tcsdup(_T("c:\somedir\myapp.exe"));

参考文献:


1. CWinApp::m_pszAppName





原文地址:https://www.cnblogs.com/lanzhi/p/6470429.html