GetOpenFileName 使用后内存未正常释放,怎么办

GetOpenFileName 使用后内存未正常释放,怎么处理?
使用GetOpenFileName 后发现无法正常释放内存,一直被占用着。有什么方法可以解决。

测试方法:
    1、新建一个标准win32 c++ 工程,
    2、增加一个selectFileName的函数,调用GetOpenFileName
    3、在winproc里增加双击调用selectFileName

Debug方式启动时程序占用 5M,调用selectFileName急剧蹿升到25M,关闭对话框后回落到  23M,无法回落到先前的值。
占用近 18M,有些不敢想象。
GetOpenFileName 方法直接从MSDN上复制过来的。

有什么方法可以处理内存占用问题。


测试环境:
     win10
     visual studio 2015


GetOpenFileName 使用后内存未正常释放,怎么办

// Win32Project1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project1.h"
#include <Windows.h>
#include <commdlg.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
TCHAR szFile[260];       // buffer for file name
DWORD nOpenErr = 0;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void                selectFileName(HWND hwnd);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

  // TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
    LoadString(hInstance, IDS_APP_TITLE, szFile, 260);

MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
    TCHAR buf[100];

switch (message)
{
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
        SetTextColor(hdc, GetSysColor(COLOR_WINDOW));
        SetBkColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
        ExtTextOut(hdc, 10, 10, ETO_OPAQUE, 0, szFile, wcslen(szFile), 0);
        wsprintf(buf, L"Err: %d", nOpenErr);
        ExtTextOut(hdc, 10, 30, ETO_OPAQUE, 0, buf, wcslen(buf), 0);

EndPaint(hWnd, &ps);
break;
    case WM_LBUTTONDBLCLK:
        selectFileName(hWnd);
        InvalidateRect(hWnd, NULL, FALSE);
        break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

void selectFileName(HWND hwnd)
{
    OPENFILENAME ofn;       // common dialog box structure
    HANDLE hf;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = 0;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = L'\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    // Display the Open dialog box. 
    GetOpenFileName(&ofn);
    nOpenErr = CommDlgExtendedError();

}

------解决思路----------------------
没注意过这个

是不是第一运行初始化了一些全局的东西,这个没法释放
你试试打开多次,看 内存怎么涨的
如果是n*18,那确实有问题
如果不再增长那就说明没问题
------解决思路----------------------
参考MSDN98中相关例子代码。
MSDN98_1.ISO http://pan.baidu.com/s/1dDF41ix,  MSDN98_2.ISO http://pan.baidu.com/s/1bnGo0Vl

------解决思路----------------------
反复调用 GetOpenFileName  看内存有没有持续增加,如果持续增加的就有问题了。 如果虽然关闭后没释放完,第二次调用和第一次调用后的差不多就算正常。
------解决思路----------------------
引用:
反复打开基本没有增加,最多增加1~2M左右。会回落到23M左右的位置。
GetOpenFileName 使用后内存未正常释放,怎么办太悲剧了,全局竟然要占用这么多。

唉~现在的程序越来越吃内存。俺去装个MSND98看看例子。

现在的机器内存越来越大了。如硬件内存很有限,应该使用专用的操作系统。
------解决思路----------------------
这么点代码打开就占用 5M 就已经醉了,幸好我装的不是 2013
------解决思路----------------------
引用:
反复打开基本没有增加,最多增加1~2M左右。会回落到23M左右的位置。
GetOpenFileName 使用后内存未正常释放,怎么办太悲剧了,全局竟然要占用这么多。

唉~现在的程序越来越吃内存。俺去装个MSND98看看例子。


那没问题。 内存申请的时候, 只有在free list没有内存的时候, 向OS申请内存。 23MB - 5MB = 18MB 估计在放在free list了。