Archived Forum Post

Index of archived forum posts

Question:

I use CkSettings::cleanupMemory() but still get memory leaks

Nov 15 '12 at 00:32

I'm using I use CkSettings::cleanupMemory() at the end of my MFC dialog based program but still get memory leaks when the program exits.

I'm using a simple call to CkHttp in a separate thread. Without that call there are no memory leaks.

I've tried calling cleanupMemory() at the end of the thread and at the end of the program, neither seem to work...


Answer

If making a Chilkat method call that returns an object, such as a "CkHttpResponse *" object, make sure to delete it before your program exits. For example:

CkHttpResponse *resp = http.SynchronousRequest( ...  );

...

delete resp;

Answer

If it's truly a memory leak in Chilkat, then it should be equally reproducible in a simple single-threaded example. In other words -- remove the complexity so that nothing else can be the problem. Write a very simple program that demonstrates the leak. For example:

void leakTest(void)
    {
    CkHttp http;
    bool success = http.UnlockComponent("test");
    const char *html = http.quickGetStr("http://your_url_here");
    return;
    }

#if defined(WIN32) int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) #else int main(int argc, char *argv[]) #endif {
leakTest();
// // All Chilkat objects need to have been destructed prior to calling CkSettings::cleanupMemory. // This is why the CkHttp object is placed in a function, so that when the function returns, // the CkHttp object is destructed.
CkSettings::cleanupMemory();
// #if defined(WIN32)
_CrtDumpMemoryLeaks(); #endif

return 0;

}


Answer

The answer it seems is that calling _endthreadex (in a separate thread from the main MFC one) creates the memory leaks.

Maybe the problem is that _endthreadex finishes the thread before cleanupMemory gets a chance to finish. A mystery wrapped in an enigma!