Skip to content

Commit

Permalink
降低退出时崩溃的概率
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyuanxiang committed Jan 12, 2019
1 parent 681ab9b commit 77a1de6
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 75 deletions.
4 changes: 4 additions & 0 deletions ReadMe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@

3、打开注册表关闭后崩溃,参照按对文件管理窗口的修改进行处理。遗留问题:
并无内存泄漏,但退出时报"HEAP: Free Heap modified after it was freed"问题。

4、退出时睡眠一会,等待服务端清理,发现这样可以避免退出时崩溃的概率。

5、发布稍微稳定的版本V1.0.0.1。
3 changes: 3 additions & 0 deletions client/ClientDll.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<AdditionalIncludeDirectories>./;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PreprocessorDefinitions>ZLIB_WINAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -64,6 +66,7 @@
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AdditionalIncludeDirectories>./;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>ZLIB_WINAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
3 changes: 3 additions & 0 deletions client/TestRun.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -56,6 +58,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
3 changes: 3 additions & 0 deletions client/ghost.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<AdditionalIncludeDirectories>./;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PreprocessorDefinitions>_CONSOLE;ZLIB_WINAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -69,6 +71,7 @@
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AdditionalIncludeDirectories>./;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CONSOLE;ZLIB_WINAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down
21 changes: 21 additions & 0 deletions server/2015Remote/2015Remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CMy2015RemoteApp::CMy2015RemoteApp()

// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
m_Mutex = NULL;
}


Expand All @@ -75,6 +76,14 @@ CMy2015RemoteApp theApp;

BOOL CMy2015RemoteApp::InitInstance()
{
m_Mutex = CreateMutex(NULL, FALSE, "YAMA.EXE");
if (ERROR_ALREADY_EXISTS == GetLastError())
{
CloseHandle(m_Mutex);
m_Mutex = NULL;
return FALSE;
}

SetUnhandledExceptionFilter(&whenbuged);

// 如果一个运行在 Windows XP 上的应用程序清单指定要
Expand Down Expand Up @@ -128,3 +137,15 @@ BOOL CMy2015RemoteApp::InitInstance()
// 而不是启动应用程序的消息泵。
return FALSE;
}


int CMy2015RemoteApp::ExitInstance()
{
if (m_Mutex)
{
CloseHandle(m_Mutex);
m_Mutex = NULL;
}

return CWinApp::ExitInstance();
}
2 changes: 2 additions & 0 deletions server/2015Remote/2015Remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class CMy2015RemoteApp : public CWinApp
public:
CMy2015RemoteApp();
iniFile m_iniFile;
HANDLE m_Mutex;
// 重写
public:
virtual BOOL InitInstance();

// 实现

DECLARE_MESSAGE_MAP()
virtual int ExitInstance();
};

extern CMy2015RemoteApp theApp;
Binary file modified server/2015Remote/2015Remote.rc
Binary file not shown.
8 changes: 6 additions & 2 deletions server/2015Remote/2015Remote.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;_CRT_SECURE_NO_WARNINGS;ZLIB_WINAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -76,10 +78,12 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<Optimization>MinSpace</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;_CRT_SECURE_NO_WARNINGS;ZLIB_WINAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
<StringPooling>true</StringPooling>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
69 changes: 43 additions & 26 deletions server/2015Remote/2015RemoteDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,6 @@ CMy2015RemoteDlg::CMy2015RemoteDlg(CWnd* pParent /*=NULL*/)

CMy2015RemoteDlg::~CMy2015RemoteDlg()
{
Sleep(200);
EnterCriticalSection(&m_cs);
for (std::vector<CFileManagerDlg *>::iterator iter = v_FileDlg.begin();
iter != v_FileDlg.end(); ++iter)
{
CFileManagerDlg *cur = *iter;
::SendMessage(cur->GetSafeHwnd(), WM_CLOSE, 0, 0);
while (false == cur->m_bIsClosed)
Sleep(1);
delete cur;
}
for (std::vector<CRegisterDlg *>::iterator iter = v_RegDlg.begin();
iter != v_RegDlg.end(); ++iter)
{
CRegisterDlg *cur = *iter;
::SendMessage(cur->GetSafeHwnd(), WM_CLOSE, 0, 0);
while (false == cur->m_bIsClosed)
Sleep(1);
delete cur;
}
LeaveCriticalSection(&m_cs);
DeleteCriticalSection(&m_cs);
}

Expand Down Expand Up @@ -262,6 +241,7 @@ VOID CMy2015RemoteDlg::CreatStatusBar()

VOID CMy2015RemoteDlg::CreateNotifyBar()
{
#if SHOW_NOTIFY
m_Nid.cbSize = sizeof(NOTIFYICONDATA); //大小赋值
m_Nid.hWnd = m_hWnd; //父窗口 是被定义在父类CWnd类中
m_Nid.uID = IDR_MAINFRAME; //icon ID
Expand All @@ -271,6 +251,7 @@ VOID CMy2015RemoteDlg::CreateNotifyBar()
CString strTips ="禁界: 远程协助软件"; //气泡提示
lstrcpyn(m_Nid.szTip, (LPCSTR)strTips, sizeof(m_Nid.szTip) / sizeof(m_Nid.szTip[0]));
Shell_NotifyIcon(NIM_ADD, &m_Nid); //显示托盘
#endif
}

VOID CMy2015RemoteDlg::CreateToolBar()
Expand Down Expand Up @@ -438,6 +419,10 @@ BOOL CMy2015RemoteDlg::OnInitDialog()

ListenPort();

#if !SHOW_NOTIFY
ShowWindow(SW_SHOW);
#endif

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

Expand All @@ -448,6 +433,12 @@ void CMy2015RemoteDlg::OnSysCommand(UINT nID, LPARAM lParam)
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
#if !SHOW_NOTIFY
else if(nID == SC_CLOSE || nID == SC_MINIMIZE)
{
ShowWindow(SW_HIDE);
}
#endif
else
{
CDialogEx::OnSysCommand(nID, lParam);
Expand Down Expand Up @@ -596,8 +587,34 @@ void CMy2015RemoteDlg::OnClose()
// TODO: 在此添加消息处理程序代码和/或调用默认值
KillTimer(0);

#if SHOW_NOTIFY
Shell_NotifyIcon(NIM_DELETE, &m_Nid);
#endif

EnterCriticalSection(&m_cs);
for (std::vector<CFileManagerDlg *>::iterator iter = v_FileDlg.begin();
iter != v_FileDlg.end(); ++iter)
{
CFileManagerDlg *cur = *iter;
::SendMessage(cur->GetSafeHwnd(), WM_CLOSE, 0, 0);
while (false == cur->m_bIsClosed)
Sleep(1);
delete cur;
}
for (std::vector<CRegisterDlg *>::iterator iter = v_RegDlg.begin();
iter != v_RegDlg.end(); ++iter)
{
CRegisterDlg *cur = *iter;
::SendMessage(cur->GetSafeHwnd(), WM_CLOSE, 0, 0);
while (false == cur->m_bIsClosed)
Sleep(1);
delete cur;
}
LeaveCriticalSection(&m_cs);

//加上下面Sleep语句能避免不少退出时的崩溃、怀疑是IOCP需要背地干些工作
ShowWindow(SW_HIDE);
Sleep(500);

if (m_iocpServer!=NULL)
{
Expand Down Expand Up @@ -788,13 +805,13 @@ void CMy2015RemoteDlg::OnMainExit()

VOID CMy2015RemoteDlg::ListenPort()
{
int nPort = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("Settings", "ListenPort");
int nPort = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "ghost");
//读取ini 文件中的监听端口
int nMaxConnection = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("Settings", "MaxConnection");
int nMaxConnection = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "MaxConnection");
//读取最大连接数
if (nPort == 0)
nPort = 2356;
if (nMaxConnection == 0)
if (nPort<=0 || nPort>65535)
nPort = 6543;
if (nMaxConnection <= 0)
nMaxConnection = 10000;
Activate(nPort,nMaxConnection); //开始监听
}
Expand Down
9 changes: 9 additions & 0 deletions server/2015Remote/2015RemoteDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include "TrueColorToolBar.h"
#include "IOCPServer.h"

//////////////////////////////////////////////////////////////////////////
// 以下为特殊需求使用
// 是否显示托盘
#define SHOW_NOTIFY 1

//////////////////////////////////////////////////////////////////////////

typedef struct _LOGIN_INFOR
{
BYTE bToken; // = 1 //登陆信息
Expand Down Expand Up @@ -63,7 +70,9 @@ class CMy2015RemoteDlg : public CDialogEx

CStatusBar m_StatusBar; //状态条
CTrueColorToolBar m_ToolBar;
#if SHOW_NOTIFY
NOTIFYICONDATA m_Nid;
#endif
CRITICAL_SECTION m_cs;

UINT m_iCount;
Expand Down
6 changes: 3 additions & 3 deletions server/2015Remote/IOCPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ IOCPServer::IOCPServer(void)
m_hListenEvent = WSA_INVALID_EVENT;
m_hListenThread = INVALID_HANDLE_VALUE;

m_ulMaxConnections = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("Settings", "MaxConnection");
m_ulMaxConnections = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "MaxConnection");

if (m_ulMaxConnections==0)
if (m_ulMaxConnections<=0)
{
m_ulMaxConnections = 100;
m_ulMaxConnections = 10000;
}

InitializeCriticalSection(&m_cs);
Expand Down
12 changes: 6 additions & 6 deletions server/2015Remote/SettingDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ BOOL CSettingDlg::OnInitDialog()
{
CDialog::OnInitDialog();

int nPort = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("Settings", "ListenPort");
int nPort = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "ghost");
//读取ini 文件中的监听端口
int nMaxConnection = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("Settings", "MaxConnection");
int nMaxConnection = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "MaxConnection");

m_nListenPort = nPort;
m_nMax_Connect = nMaxConnection;
m_nListenPort = (nPort<=0 || nPort>65535) ? 6543 : nPort;
m_nMax_Connect = nMaxConnection<=0 ? 10000 : nMaxConnection;

UpdateData(FALSE);

Expand All @@ -60,9 +60,9 @@ BOOL CSettingDlg::OnInitDialog()
void CSettingDlg::OnBnClickedButtonSettingapply()
{
UpdateData(TRUE);
((CMy2015RemoteApp *)AfxGetApp())->m_iniFile.SetInt("Settings", "ListenPort", m_nListenPort);
((CMy2015RemoteApp *)AfxGetApp())->m_iniFile.SetInt("settings", "ghost", m_nListenPort);
//向ini文件中写入值
((CMy2015RemoteApp *)AfxGetApp())->m_iniFile.SetInt("Settings", "MaxConnection", m_nMax_Connect);
((CMy2015RemoteApp *)AfxGetApp())->m_iniFile.SetInt("settings", "MaxConnection", m_nMax_Connect);

m_ApplyButton.EnableWindow(FALSE);
m_ApplyButton.ShowWindow(SW_HIDE);
Expand Down
43 changes: 8 additions & 35 deletions server/2015Remote/iniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,26 @@ iniFile::iniFile(void)

BOOL iniFile::ContructIniFile()
{
char szFilePath[MAX_PATH] = {0};
char* FindPoint = NULL;

char szFilePath[MAX_PATH] = {0}, *p = szFilePath;
::GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
while (*p) ++p;
while ('\\' != *p) --p;
strcpy(p+1, "settings.ini");

FindPoint = strrchr(szFilePath,'.');
if (FindPoint!=NULL)
{
*FindPoint = '\0';
strcat(szFilePath,".ini");
}

m_IniFilePath = szFilePath; //赋值给文件名 查看 一个成员函数 IniFileName

HANDLE hFile = CreateFileA(m_IniFilePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); //同步 异步

if (hFile==INVALID_HANDLE_VALUE)
{
return FALSE;
}

ULONG ulLow = GetFileSize(hFile,NULL);

if (ulLow>0)
{
CloseHandle(hFile);

return FALSE;
}

CloseHandle(hFile);

WritePrivateProfileString("Settings", "ListenPort","2356",m_IniFilePath);
WritePrivateProfileString("Settings", "MaxConnection","10000",m_IniFilePath);
m_IniFilePath = szFilePath;

return TRUE;
}

int iniFile::GetInt(CString MainKey,CString SubKey) //"Setting" "ListenPort"
int iniFile::GetInt(CString MainKey,CString SubKey)
{
return ::GetPrivateProfileInt(MainKey, SubKey,0,m_IniFilePath);
}

BOOL iniFile::SetInt(CString MainKey,CString SubKey,int Data)//8888
BOOL iniFile::SetInt(CString MainKey,CString SubKey,int Data)
{
CString strData;
strData.Format("%d", Data); //2356
strData.Format("%d", Data);
return ::WritePrivateProfileString(MainKey, SubKey,strData,m_IniFilePath);
}

Expand Down
Loading

0 comments on commit 77a1de6

Please sign in to comment.