I have experienced a strange problem in windows vista and above. When I use the IFileOpenDialog with a large stack, the amount of memory remaining after showing the dialog drops by about a gigabyte.
#include <windows.h>
#include <Shobjidl.h>
#include <iostream>
void memGrab(char *);
int main(int argc, char **argv){
char Message[128];
CoInitialize(NULL);
if(argc > 1){
wsprintf(Message, "Starting Memory Test: %c", argv[1][0]);
std::cout << Message << "\n";
if(argv[1][0] == 'b')
memGrab("before");
TCHAR *fileName = (TCHAR *)malloc(sizeof(TCHAR) * 1024);
IFileOpenDialog *pfd;
int index = 0;
int len = 0;
int i = 0;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pfd));
if(SUCCEEDED(hr)){
pfd->Show(NULL);
}
pfd->Release();
if(argv[1][0] == 'a')
memGrab("After");
}
CoUninitialize();
}
void memGrab(char *text){
for(int i = 0; i < 400000; i++){
if(!malloc(10240)){
char Message[128];
wsprintf(Message, "Memory Gone %s: %d K", text, i * 10);
std::cout << Message << "\n";
exit(0);
}
}
}
When I compile this program without setting a stack size (cl testMem.cpp Shell32.lib Ole32.lib user32.lib
). I get the following result:
C:\RWSource>testMem.exe b
Starting Memory Test: b
Memory Gone before: 1984040 K
C:\RWSource>testMem.exe a
Starting Memory Test: a
Memory Gone After: 1875640 K
However, when I compile it with setting the stack size (cl testMem.cpp Shell32.lib Ole32.lib user32.lib /F100000000
). I loose a large amount of allocatable memory.
C:\RWSource>testMem.exe b
Starting Memory Test: b
Memory Gone before: 1795370 K
C:\RWSource>testMem.exe a
Starting Memory Test: a
Memory Gone After: 463840 K
Update 1:
I checked the memory with VMMap (thank you TheUndeadFish) and when I open the dialog, there are multiple threads started that all have about 100MB. Is there any way to make the main thread have a large stack while not giving the child threads 100MB each?