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

Threaded memory allocator

16 views
Skip to first unread message

David N. Welton

unread,
Mar 26, 2003, 7:31:43 AM3/26/03
to

What's the best way to work around this:

int Crashtcl_Init(Tcl_Interp *interp)
{
char *foo = Tcl_Alloc(10);
char *bar = NULL;
bar = strdup(foo);
Tcl_Free(bar);

return TCL_OK;
}

@ashland [~/tmp] $ gcc -fPIC -shared -Wl,-soname,libcrashtcl.so -o libcrashtcl.so crashtcl.c
@ashland [~/tmp] $ tclsh
% load ./libcrashtcl.so
alloc: invalid block: 0x10078de8: 20 65 73

Aborted (core dumped)

When it does the strdup, the 'magic number' is being discarded. The
conclusion I draw from this is that you can't use normal c functions
like strdup with Tcl_Alloc'ed memory. Am I mistaken? It's pretty
late at night, so it seems a distinct possibility.

--
David N. Welton
Consulting: http://www.dedasys.com/
Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
Apache Tcl: http://tcl.apache.org/

David N. Welton

unread,
Mar 26, 2003, 5:08:05 PM3/26/03
to
dav...@dedasys.com (David N. Welton) writes:

> When it does the strdup, the 'magic number' is being discarded. The
> conclusion I draw from this is that you can't use normal c functions
> like strdup with Tcl_Alloc'ed memory. Am I mistaken? It's pretty
> late at night, so it seems a distinct possibility.

Since I've received several replies in private, I'll sum up the
conclusion, so that anyone interested can read about it:

You can't mix Tcl_Free/Tcl_Alloc with regular free/malloc/strdup. It
may work fine in some cases, but there are others where it will panic.

marsd

unread,
Mar 27, 2003, 1:08:19 PM3/27/03
to
dav...@dedasys.com (David N. Welton) wrote in message news:<87isu5d...@dedasys.com>...

> dav...@dedasys.com (David N. Welton) writes:
>
> > When it does the strdup, the 'magic number' is being discarded. The
> > conclusion I draw from this is that you can't use normal c functions
> > like strdup with Tcl_Alloc'ed memory. Am I mistaken? It's pretty
> > late at night, so it seems a distinct possibility.
>
> Since I've received several replies in private, I'll sum up the
> conclusion, so that anyone interested can read about it:
>
> You can't mix Tcl_Free/Tcl_Alloc with regular free/malloc/strdup. It
> may work fine in some cases, but there are others where it will panic.

Okay, I'll bite on a related point.
I've noticed some of the regulars casting tcl_alloc, where in
my experience with C, casting malloc is frowned upon.
Is it necessary to cast tcl_alloc?

Joe English

unread,
Mar 27, 2003, 5:37:20 PM3/27/03
to
marsd wrote:
>
>I've noticed some of the regulars casting tcl_alloc, where in
>my experience with C, casting malloc is frowned upon.
>Is it necessary to cast tcl_alloc?


When Tcl was invented, K&R C was still the norm, so Tcl_Alloc()
and family return 'char *' instead of 'void *'.

Nobody ever got around to changing this, so Tcl_Alloc() still
needs a cast.


--Joe English

jeng...@flightlab.com

Alex Davies

unread,
Mar 28, 2003, 3:24:47 AM3/28/03
to
dav...@dedasys.com (David N. Welton) wrote in message news:<87isu5d...@dedasys.com>...
> dav...@dedasys.com (David N. Welton) writes:
>
> > When it does the strdup, the 'magic number' is being discarded. The
> > conclusion I draw from this is that you can't use normal c functions
> > like strdup with Tcl_Alloc'ed memory. Am I mistaken? It's pretty
> > late at night, so it seems a distinct possibility.
>
> Since I've received several replies in private, I'll sum up the
> conclusion, so that anyone interested can read about it:
>
> You can't mix Tcl_Free/Tcl_Alloc with regular free/malloc/strdup. It
> may work fine in some cases, but there are others where it will panic.

Could someone expand more on this.

I thought Tcl_Alloc/Tcl_Free were merely wrappers around malloc/free,
which would suggest that there would be no problems mixing the two.

TIA

alex.

Zoran Vasiljevic

unread,
Mar 28, 2003, 8:55:03 AM3/28/03
to
alex_...@3b2.com (Alex Davies) wrote in message news:<22d5ab35.03032...@posting.google.com>...

> I thought Tcl_Alloc/Tcl_Free were merely wrappers around malloc/free,
> which would suggest that there would be no problems mixing the two.

Well, no. Tcl has its own memory manager which produces
better results when used in mt-apps.
Mixing malloc & friends with their Tcl_* counterparts is
not going to work. Either you use Tcl_Alloc/Tcl_Free or
malloc/free but do not mix.

Cheers,
Zoran

Jeffrey Hobbs

unread,
Mar 30, 2003, 12:14:37 PM3/30/03
to
David N. Welton wrote:
> dav...@dedasys.com (David N. Welton) writes:
>>When it does the strdup, the 'magic number' is being discarded. The
>>conclusion I draw from this is that you can't use normal c functions
>>like strdup with Tcl_Alloc'ed memory. Am I mistaken? It's pretty
>>late at night, so it seems a distinct possibility.

> You can't mix Tcl_Free/Tcl_Alloc with regular free/malloc/strdup. It


> may work fine in some cases, but there are others where it will panic.

This has always been true. The default for 8.4 uses the system malloc
and free only in the non-threaded builds for Windows and Unix, but
even then someone can add -DTCL_USEALLOC=1 to the build and have it
use the special-purpose built-in memory allocator in Tcl. Threaded
builds use their own special malloc that is designed for
high-performance in the threaded case.

--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions

0 new messages