If I have two threads each doing this bit of code, they die . . .
void do_stuff() // Nothing special
{
char *a=(char *)malloc(1024*1024); // Allocate a meg of memory
free a; // Deallocate that memory
}
I can't figure out why? Any sugestions?
I do know that if I create a global mutex and do this . . .
void do_stuff() // Nothing special
{
Mutex.lock();
char *a=(char *)malloc(1024*1024); // Allocate a meg of memory
Mutex.unlock();
Mutex.lock();
free a; // Deallocate that memory
Mutex.unlock();
}
This works (duh of course).
Has anyone run into this before?
Thanks.
Jack Strohm wrote in message ...
Yes; then I quickly found out about the /MT and /MTd compiler switches. :)
In the Microsoft environment, you have different versions of the basic C
library; one for linking with single-threaded programs and one for
multi-threaded. (With debugging versions for both).
>If I have two threads each doing this bit of code, they die . . .
>
>void do_stuff() // Nothing special
>{
> char *a=(char *)malloc(1024*1024); // Allocate a meg of memory
> free a; // Deallocate that memory
>}
>
>I can't figure out why? Any sugestions?
Maybe you are working in C and forgot to include <stdlib.h>. The
(char *) cast in the malloc() call is a wonderful way to shut up the
compiler when it wants to complain about assigning an int to a char *.
>I do know that if I create a global mutex and do this . . .
>
>void do_stuff() // Nothing special
>{
> Mutex.lock();
> char *a=(char *)malloc(1024*1024); // Allocate a meg of memory
> Mutex.unlock();
>
> Mutex.lock();
> free a; // Deallocate that memory
> Mutex.unlock();
>}
>
>This works (duh of course).
Oh. In that case, have a look at those compiler switches.
If you are using the Microsoft DevStudio IDE, you can use the project settings
dialog to select whether you are building a multi-threaded executable or not.
Pick the 'C/C++' tab, then select ``Code generation'' in the ``Category:''
combo box. Then in the ``Use run-time library:'' combo box pick the
appropriate choice. Unfortunately, you have to do this individually for all
your various configurations; i.e. pick all the debugging ones one by one and
change the library to ``Debug multithreaded'', then pick all the release ones
and change to ``Multithreaded''.
Are you compiling and linking with the multithreaded libraries?
Use /MD or /MT.
) free a; // Deallocate that memory
Is this a typo? You need parentheses around the function's
arguments. This is C, not Visual Basic.
) free(a); // Deallocate that memory
--
/George V. Reilly mailto:g...@halcyon.com
ATL, Vim (Vi IMproved), Active Server Pages: http://george.reilly.org
Co-author, "Beginning ATL COM Programming", Wrox Press, 1998
I believe that should be /MTd rather than /MD .
Nope.
C:>cl /?
[deletia]
-LINKING-
/MD link with MSVCRT.LIB /MDd link with MSVCRTD.LIB debug lib
/ML link with LIBC.LIB /MLd link with LIBCD.LIB debug lib
/MT link with LIBCMT.LIB /MTd link with LIBCMTD.LIB debug lib
Linking with the DLL version of the CRT (MSVCRT) automatically
uses the threadsafe version of the APIs.
Okay, sorry! :)
"George V. Reilly" a écrit :
> Jack Strohm <jst...@netdoor.com> wrote:
> ) Has anyone done multithreaed programming in Windows found problems with
> ) malloc and free not being thread safe?
> )
> ) If I have two threads each doing this bit of code, they die . . .
> )
> ) void do_stuff() // Nothing special
> ) {
> ) char *a=(char *)malloc(1024*1024); // Allocate a meg of memory
> ) free a; // Deallocate that memory
> ) }
> )
> ) I can't figure out why? Any sugestions?
>
> Are you compiling and linking with the multithreaded libraries?
> Use /MD or /MT.
>
> ) free a; // Deallocate that memory
>
> Is this a typo? You need parentheses around the function's
> arguments. This is C, not Visual Basic.
>
> ) free(a); // Deallocate that memory
>
> --
> /George V. Reilly mailto:g...@halcyon.com
> ATL, Vim (Vi IMproved), Active Server Pages: http://george.reilly.org
> Co-author, "Beginning ATL COM Programming", Wrox Press, 1998
You must use the WIN32 allocation functions :
HGLOBAL hMem;
char *ptr;
/* memory allocation */
hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, 1024*1024);
ptr = (char *) GlobalLock(hMem);
if (ptr == NULL)
/* there is a problem */
......
/* Free allocation */
GlobalUnlock(hMem);
GlobalFree(hMem);
--
------------------------------------------------------------------
Mail : Patrick...@Wanadoo.fr
Web : http://perso.wanadoo.fr/patrick.deiber/
------------------------------------------------------------------
> You must use the WIN32 allocation functions :
>
No. GlobalAlloc & GlobalLock etc should not be used. If u do want to use Win32
funcs then u should use VirtualAlloc and VirtualFree. Actually these two are
the only true memory allcoation/deallocation functions in win32. rest just use
tehse internally.
Anil Gupta
-----------