Results 1 to 9 of 9

Thread: Pointer/Memory Leak

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Pointer/Memory Leak

    Hope you guy can give answer my problem.

    PHP Code:
    void functiona (HWND hWnd)
    {
       
    char *lpszMesage=NULL;
       
    char *lpszTmp=NULL;
       
    char *lpszPtr=NULL;

       
    lpszMessage = new char[14];
       
    strcpy(lpszMessage"1234567890..."); 
       
    lpszPtr lpszMessage
       lpszPtr
    ++;   
       
    MessageBox(hWndlpszPtr"Data #1"MB_OK);
       
    lpszTmp = new char[10];
       
    strcpy(lpszTmp"123456789");
       
    lpszPtr lpszPtr 5;       
       
    lpszPtr lpszTmp;
       
    MessageBox(hWndlpszPtr"Data #2"MB_OK);
       
    delete [] lpszTmp;
       
    delete [] lpszMessage;

    I notice that there will be 4Bytes of memory leak on each time i run this code. Is this due to the lpszPtr variable is not been delete?

    regards,
    Chris.C

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I don't see any internal memory leaks and this line:
    lpszPtr = lpszPtr + 5;
    seems to be redudant
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    The lpszPtr = lpszPtr just some dummy action. can be ignore
    How about this?

    PHP Code:
    DWORD WINAPI Functiona (LPVOID pParam)
    {
       
    char *lpszMesage=NULL;
       
    char *lpszTmp=NULL;
       
    char *lpszPtr=NULL;
        
       
    TCHAR *lpszData = (TCHAR*)pParam;
       
    lpszMessage = new char[14];
       
    strcpy(lpszMessagelpszData); 
       
    lpszPtr lpszMessage
       lpszPtr
    ++;   
       
    MessageBox(hWndlpszPtr"Data #1"MB_OK);
       
    lpszTmp = new char[10];
       
    strcpy(lpszTmp"123456789");
       
    lpszPtr lpszPtr 5;       
       
    lpszPtr lpszTmp;
       
    MessageBox(hWndlpszPtr"Data #2"MB_OK);
       
    delete [] lpszTmp;
       
    delete [] lpszMessage;
       
       
    ExitThread(WM_QUIT);


  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ah, remove exitthread, better let it terminate manually, reason why you a have memory leak
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Just wanna to know why the ExitThread will cause memory leak?
    Is that the function to terminate a thread?

    What the different between terminate a thread with return 0 and ExitThread(WM_QUIT)?

    Thx in advance

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    a) WM_QUIT is not 0. The return code of the thread will therefore not be 0.

    b) ExitThread IMMEDIATLY exits the thread. It doesn't allow the CRT to clean up it's resources (assuming you used _beginthreadex to start the thread, which you should do if you use CRT functions in the thread), no object destructors to be called and no memory that was reserved to be freed (although if you forget to free anything it's lost until app termination anyway, but windows may use resources too).

    Just out of interest, how did you detect the leak?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    em... I use the CreateThread API to create a thread as below:

    PHP Code:
       TCHAR    *lpszMessage=NULL;
       
    HANDLE  hThread=NULL;
       
    DWORD  dwTID=0;
        
       
    lpszMessage = new TCHAR[20];
       
    memset(lpszMessageTEXT('\0'), 20);
       
    strcpy(lpszMessageTEXT("123456789A123456789B"));
       
    hThread CreateThread(NULL0FunctionalpszMessage0, &dwTID);
       
    Sleep(50);
       
    CloseHandle(hThread);
       
    delete [] lpszMessage
    I check the memory status on the task manager or (system on WinCE) after everytimes I execute the thread. Most of the time will be under WindowsCE environment, but it should be the same as normal window NT platform.

    ea... is the CRT mean Critical Section?

    So, CornedBee & kedaman both of you are recomend to use return 0; instead of ExitThread to terminate a thread it self?

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    taskmanager only shows memory usage in kb for me, i'm using win2k, i guess you can use the performance counters, i checked them out quite a while ago...
    yeah return to halt the thread and you'll be fine.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    CRT is the C runtime library (like strcpy, malloc...). Especially the functions strtok and asctime are extremely endangered whenever used in a thread without _beginthreadex (use process.h).

    Code:
       hThread = CreateThread(NULL, 0, Functiona, lpszMessage, 0, &dwTID);
       Sleep(50);
       CloseHandle(hThread);
       delete [] lpszMessage;
    That piece of code is extremely dangerous. Although it is highly likely, you can't guarantee that the created thread will get a chance to execute any code before Sleep returns. This way, lpszMessage would be deleted before the thread accesses it. When it then does, it will cause an access violation.

    It would be better if the thread frees the memory itself just before finishing. Or let the primary thread wait for the other thread's end with WaitForSingleObject. Rather a bad idea cause you can't continue with code during this time which makes multithreading rather senseless.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width