Thread: Memory Problem with _beginthread()

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Question Memory Problem with _beginthread()

    Hello,

    i have a confusing memory problem using multithreading with a .DLL file.

    Code:
    extern "C" __declspec(dllimport) void U_SchallMain(void* args); // include the main from my .DLL file
    
    int main(int argc, char *argv[])
    {
      HANDLE U_SchallHandle = (HANDLE) _beginthread( U_SchallMain, 0, (void *) 20 ); //start Thread
      WaitForSingleObject(U_SchallHandle, INFINITE);
      system("pause");
    }
    The .DLL Code is really crappy, some voodoo mathmatics stuff happens there in an infinity loop - there are also some memory leaks which i can not solve.
    My idea was to start this Code as a Thread and restart it after a certain number of iterations in order to free the memory used by the Thread.
    So far so good.

    This is the Code of the DLL:

    Code:
    int __declspec (dllexport) U_SchallMain( int cycles )
    {
      ... voodoo ...
    
      if( run >= cycles ) // stop program after number of cycles is => var 'cycles'
      {
        printf("DEBUG_U_SCHALL::Quit after %i cycles\n", run);
        _endthread();
      }
    }
    Now my problem: The Memory allocated to the Thread is not free after it calls _endthread();
    When I start my program it takes about 7.000K, with the Thread started it weights 14.000k and higher. After _endthread() it should be 7.000K again but it doesnt.

    Is it because I am using _beginthread instead of _beginthreadex or where is the mistake?

    Im using Visual Studio 2008 and compile this project as C++.

    (PS. Sorry for my bad english )

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That has absolutely nothing to do with threads. Memory allocated using malloc and friends is nearly never freed until the process is terminated. It is however, assuming you free the memory itself using free() or realloc() with zero size, recycled for other malloc calls later on.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    You are not supposed to use identifiers starting with an underscore. Do you know exactly what you are doing? My guess is that there's a nice wrapper function in the API which relies on _whatever() and does the memory handling for you.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Snafuist View Post
    You are not supposed to use identifiers starting with an underscore. Do you know exactly what you are doing? My guess is that there's a nice wrapper function in the API which relies on _whatever() and does the memory handling for you.

    Greets,
    Philip
    No, _beginthread is the official MS interface - it is named that way because it is not a standard function (or some such). Underscore names are perfectly valid - you just shouldn't name anything YOURSELF with a underscore.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with atl memory cleaning
    By Mariam1989 in forum Windows Programming
    Replies: 1
    Last Post: 11-11-2008, 12:35 PM
  2. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM