Thread: Threads, Dll

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Question Threads, Dll

    Hi, I have a small MFC app, i have two buttons, i have a dummy function too.
    I have a small dll, the issue is when i press the button 2 I have memory leak (i have tried with CMemoryState and does not detect any memory leak, but i can see it in the Process Explorer).

    In my simple dll code i have in the "stdafx.h" file the following line
    #include <afxwin.h>,
    but if i comment this line, the memory leak is gone.

    I tried to comment all my dll code (except that include) and the problem still persists.

    Any clues of this behavior?

    (I tried in a Win32 application instead of MFC and i have the same problem)


    Code:
    void CPruebaThreadDlg::OnBnClickedButton1()
    {
    	LPVOID lpParam = NULL;
    	
    	AfxBeginThread(dummy, lpParam, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
    }
    
    void CPruebaThreadDlg::OnBnClickedButton2()
    {
    	int i;
                    HMODULE hmod = LoadLibrary("SimpleDll.dll");
    	while(true)
    	{
    		for(i = 0; i <= 150; i++)
    			OnBnClickedButton1();
    		Sleep(700);
    	}
    }
    
    UINT dummy(LPVOID)
    {
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    You should probably close the dll when you're done. Maybe that's what giving you mem leak.
    Amish

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Identifying memory leaks using process explorer is a terrible thing to do. Here is how to check if your application is leaking memory.

    Start -> Run
    Type in perfmon
    You should now see a graph with a List control underneath it. Delete all entries in the List control. Now right-click in the empty List control, and select Add Counters.
    In the Performance Object drop down box, select Process. In the left ListBox, select Private Bytes. In the right ListBox, select your application's process. Now perfmon will graph the memory your application uses instead of the memory that the OS gives to your process.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Thanks for your answers,

    Actually, if i invoke FreeLibrary the memory is freed but i can not unload the library because i lost the state of my structures, so I can not do that.

    Bithub, i run the perfmon as you indicated me and the private bytes of my process increases a lot.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you comment out the line:
    Code:
    HMODULE hmod = LoadLibrary("SimpleDll.dll");
    Does your leak go away? If so, then the leak is in the SimpleDll DLL, not in the code you've shown us.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    In fact if i comment the line, the memory leak goes away.

    But my dll only has 3 files,
    SimpleDll.cpp
    Stdafx.h
    Stdafx.cpp

    SimpleDll.cpp
    //********Begin of file****************
    #include "stdafx.h"
    //********End of file*****************

    Stdafx.h
    //********Begin of file****************
    #include <afxwin.h>
    //********End of file****************

    Stdafx.cpp
    //********Begin of file****************
    #include "stdafx.h"
    //********End of file*****************

    If I comment the #include <afxwin.h>, the memory leak goes away.

    Any clues?

    Thanks a lot for your answers

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well I see no memory leak in your code. How bad is it leaking? How many more bytes does your application use every minute according to perfmon?

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    We finally concluded that is a OS issue, if i change my code to let minimize the window the memory decreases a lot, so the program is not using the memory. I also tested with Rational Purify and there is no memory leak,

    Thanks for your answers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM