From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29450 invoked by alias); 7 Feb 2002 16:14:21 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 29366 invoked from network); 7 Feb 2002 16:14:17 -0000 Received: from unknown (HELO chi-mail2.win.xcaliber.com) (199.88.171.25) by sources.redhat.com with SMTP; 7 Feb 2002 16:14:17 -0000 X-MimeOLE: Produced By Microsoft Exchange V6.0.5762.3 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: Problem using PThreads TLS from a DLL. Date: Thu, 07 Feb 2002 08:14:00 -0000 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Problem using PThreads TLS from a DLL. Thread-Index: AcGvhG6GR5+AnRtzEda9iADQt8hU6gAYRpwA From: "Aurelio Medina" To: "Ross Johnson" Cc: , "Pat Bacon" X-SW-Source: 2002/txt/msg00007.txt.bz2 I have found the real problem and a resolution for this problem. I will su= mmarize the problem and the solution that applies only if you are using the= PThreads DLL from within another DLL. Scenario: [MY-APP] uses [MY-DLL] which uses [PTHREADS-DLL] Problem: For simplicity assume you have only one thread, that is the MY-APP/MY-DLL/P= THREAD-DLL address spaces are all within the same thread. 1) MY-APP implicitly loads MY-DLL and PTHREADS-DLL. 2) MY-DLL allocates TLS data (calloc()) and sets up a destructor (pthread_k= ey_create()). 3) MY-APP does some work. 4) MY-APP calls exit(). 5) MY-DLL is unloaded and therfore its address space is invalidated. 6) PTHREADS-DLL calls the TLS destructor on memory that was in MY-DLL's add= ress space. 7) MY-APP crashes :( Solution: When MY-DLL receives a DLL_PROCESS_DETACH in DllMain() (i.e. Step 5) I free= () the TLS data and unset the destructor (pthread_key_delete()). Therefore= when PTHREADS-DLL unloads it won't call my invalid destructor function whi= ch tries to delete invalid memory. MY-DLL unloads before PTHREADS-DLL beca= use it is dependent on it. PS If you know of a better way please let me know. Anyhow hope this may help others later, Aurelio Medina -----Original Message----- From: Ross Johnson [mailto:rpj@ise.canberra.edu.au] Sent: Wednesday, February 06, 2002 9:06 PM To: Aurelio Medina Cc: pthreads-win32@sources.redhat.com; Pat Bacon Subject: Re: Problem using PThreads TLS from a DLL. Aurelio Medina wrote: >=20 > Hello all, >=20 > I've encountered a problem when using the PThreads-Win32 (PThreadVCE) > thread local storage (TLS) routines from within my own DLL. Before I > dig any deeper I would like to know if anyone has seen this and/or has > a workaround. My DLL is calloc'ing TLS data and I believe the TLS > destructors (which I have calling free) are firing within the main app > and not within my DLL. This causes the app that uses my DLL to crash > in the TLS destructor when calling free. It crashes beacuse the app is > trying to free CRT memory allocated by my DLL. Is there some PThread > function that I can call in my DLLMain to cause the TLS destructors to > fire when my DLL is detached? Do I understand the problem correctly: is this because the memory is unallocated when your DLL is unloaded and therefore free() segfaults? Are you able to join those threads before unloading your DLL? Pthread_join() will call the destructors for you when the threads exit. Ross >=20 > Thanks, > Aurelio Medina --=20 +-------------------------+---+ | Ross Johnson | | "Come down off the cross | Management & Technology |___| We can use the wood" - Tom Waits | Building 11 | | University of Canberra | eMail: rpj@ise.canberra.edu.au | ACT 2601 | WWW:=20=20 http://public.ise.canberra.edu.au/~rpj/ | AUSTRALIA | +-----------------------------+