Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multithreaded Programming in Windows - Malloc/Free

399 views
Skip to first unread message

Jack Strohm

unread,
Mar 22, 1999, 3:00:00 AM3/22/99
to
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?

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.


Purush Rudrakshala

unread,
Mar 22, 1999, 3:00:00 AM3/22/99
to
Are you linking with thread-safe run-time libraries?
-Purush

Jack Strohm wrote in message ...

Kaz Kylheku

unread,
Mar 22, 1999, 3:00:00 AM3/22/99
to
On Mon, 22 Mar 1999 01:25:56 -0600, Jack Strohm <jst...@netdoor.com> wrote:
>Has anyone done multithreaed programming in Windows found problems with
>malloc and free not being thread safe?

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''.

George V. Reilly

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
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

Kaz Kylheku

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
On 24 Mar 1999 10:12:17 -0800, George V. Reilly <g...@halcyon.com> wrote:
>Are you compiling and linking with the multithreaded libraries?
>Use /MD or /MT.

I believe that should be /MTd rather than /MD .

George V. Reilly

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
In article <slrn7fin7...@ashi.FootPrints.net>,
Kaz Kylheku <k...@ashi.FootPrints.net> wrote:
) On 24 Mar 1999 10:12:17 -0800, George V. Reilly <g...@halcyon.com> wrote:
) >Are you compiling and linking with the multithreaded libraries?
) >Use /MD or /MT.
)
) 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.

Kaz Kylheku

unread,
Mar 25, 1999, 3:00:00 AM3/25/99
to
On 24 Mar 1999 22:07:38 -0800, George V. Reilly <g...@halcyon.com> wrote:
>In article <slrn7fin7...@ashi.FootPrints.net>,
>Kaz Kylheku <k...@ashi.FootPrints.net> wrote:
>) On 24 Mar 1999 10:12:17 -0800, George V. Reilly <g...@halcyon.com> wrote:
>) >Are you compiling and linking with the multithreaded libraries?
>) >Use /MD or /MT.
>)
>) I believe that should be /MTd rather than /MD .
>
>Nope.

Okay, sorry! :)

Patrick Deiber

unread,
Mar 25, 1999, 3:00:00 AM3/25/99
to

"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/
------------------------------------------------------------------

Anil Gupta

unread,
Mar 31, 1999, 3:00:00 AM3/31/99
to

> 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
-----------

0 new messages