Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

Threaded memory allocator

16 vues
Accéder directement au premier message non lu

David N. Welton

non lue,
26 mars 2003, 07:31:4326/03/2003
à

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

non lue,
26 mars 2003, 17:08:0526/03/2003
à
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

non lue,
27 mars 2003, 13:08:1927/03/2003
à
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

non lue,
27 mars 2003, 17:37:2027/03/2003
à
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

non lue,
28 mars 2003, 03:24:4728/03/2003
à
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

non lue,
28 mars 2003, 08:55:0328/03/2003
à
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

non lue,
30 mars 2003, 12:14:3730/03/2003
à
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 nouveau message