Archived Forum Post

Index of archived forum posts

Question:

C++ Memory Leak Detection

Apr 24 '13 at 09:36

My program is a multithreaded 32-Bit MFC application compiled with VC10 in Unicode. It uses multiple DLLs (MFC and Win32) which internally also use some of the Chilkat libs. Some of the DLLs are loaded dynamically.

At shutdown memory leaks are reported. I want to avoid that because I need to find my own real memory leaks. I also read the support articles about that on the support pages.

Now my question:

Where in my program has CkSettings::initializeMultithreaded() to be called (in main of the exe AND/OR in each main of the DLLs) ?

Where in my program has CkSettings:: cleanupMemory() to be called (at end of the exe AND/OR at end of each DLL) ?


Answer

The CkSettings::initializeMultithreaded should only be called once from main at the start of your program prior to any other Chilkat calls, and prior to starting any other threads.

The CkSettings:: cleanupMemory() should be called only from main at the very end of your program, after all Chilkat objects have been destructed, and after all threads that might be using Chilkat objects have ended. Note: You would never want to declare a Chilkat object as a global static variable because it would be destructed after the call to cleanupMemory. Likewise, you would never declare a Chilkat object as a local var within main at the same level where cleanupMemory is called -- because it won't be destructed until after cleanupMemory is called. For example, the following code would be bad:

CkSsh myGlobalSsh;   // This is bad.

int main(int argc, char *argv[]) { CkZip zip; // This is also bad..

// Both Chilkat objects in this sample code are not destructed until AFTER cleanupMemory is called. // This should be avoided. CkSettings::cleanupMemory(); }