theOranger 2017-06-22 03:39 采纳率: 0%
浏览 1044
已结题

[window api] showwindow() 存在内存泄露

用window api 不断地创建窗口显示窗口释放窗口,会存在内存泄露,查了很多资料都没有找到解决办法,有没有大牛帮一下忙?

void CTest1Dlg::initWnd(int screenWidth, int screenHeight, HWND parent)
{
WNDCLASSEX wc;

int posX = 0, posY = 0;


// Get an external pointer to this object.  
//ApplicationHandle = this;

// Get the instance of this application.
m_hinstance = GetModuleHandle(NULL);

// Give the application a name.
m_applicationName = L"Ticker";

// Setup the windows class with default settings.
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hinstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = m_applicationName;
wc.cbSize = sizeof(WNDCLASSEX);

// Register the window class.
RegisterClassEx(&wc);

// Create the window with the screen settings and get the handle to it.
m_hwnd = CreateWindow(m_applicationName, m_applicationName,
    WS_POPUP,
    posX, posY, screenWidth, screenHeight, parent, NULL, m_hinstance, NULL);
//::SetProcessWorkingSetSize(m_hwnd,-1,-1);
::ShowWindow(m_hwnd, SW_HIDE);

}

void CTest1Dlg::ReleaseWnd()
{

::DestroyWindow(m_hwnd);
m_hwnd = NULL;
if (!UnregisterClass(m_applicationName, m_hinstance))
{
    auto err = GetLastError();
}
if (m_hinstance != NULL)
{
    FreeLibrary(m_hinstance);

    m_hinstance = NULL;
}

}

void CTest1Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//CDialogEx::OnOK();
HWND hWnd = this->GetSafeHwnd();

while (1)
{
    initWnd(100, 100, hWnd);
    ::ShowWindow(m_hwnd, SW_SHOW);
    ReleaseWnd();
    //Sleep(100);
}

}

  • 写回答

1条回答 默认 最新

  • oyljerry 2017-06-22 09:39
    关注

    窗口资源可能没有完全回收,但是你不应该反复的创建销毁窗口啊。如果只是显示隐藏,可以对同一个窗口进行显示隐藏就可以了

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)