public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* RE: Problem using PThreads TLS from a DLL.
@ 2002-02-07  8:14 Aurelio Medina
  2002-02-07  9:06 ` mmh
  0 siblings, 1 reply; 5+ messages in thread
From: Aurelio Medina @ 2002-02-07  8:14 UTC (permalink / raw)
  To: Ross Johnson; +Cc: pthreads-win32, Pat Bacon


I have found the real problem and a resolution for this problem.  I will summarize 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/PTHREAD-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_key_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 address 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 which tries to delete invalid memory.  MY-DLL unloads before PTHREADS-DLL because 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:
> 
> Hello all,
> 
> 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

> 
> Thanks,
> Aurelio Medina

-- 
+-------------------------+---+
| 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:  
http://public.ise.canberra.edu.au/~rpj/
| AUSTRALIA                   |
+-----------------------------+

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem using PThreads TLS from a DLL.
  2002-02-07  8:14 Problem using PThreads TLS from a DLL Aurelio Medina
@ 2002-02-07  9:06 ` mmh
  0 siblings, 0 replies; 5+ messages in thread
From: mmh @ 2002-02-07  9:06 UTC (permalink / raw)
  To: Aurelio Medina; +Cc: Ross Johnson, pthreads-win32, Pat Bacon

    If you have written the test code which you explained, it would be 
great if you could post the code.

    I was just wondering what was specified as the second argument to 
function pthread_key_create(). Was it a function in MY-DLL or just free()?

                MMH

>I have found the real problem and a resolution for this problem.  I will summarize 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/PTHREAD-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_key_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 address 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 which tries to delete invalid memory.  MY-DLL unloads before PTHREADS-DLL because 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:
>
>>Hello all,
>>
>>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
>
>>Thanks,
>>Aurelio Medina
>>
>




_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Problem using PThreads TLS from a DLL.
@ 2002-02-07 10:27 Aurelio Medina
  0 siblings, 0 replies; 5+ messages in thread
From: Aurelio Medina @ 2002-02-07 10:27 UTC (permalink / raw)
  To: hanasim; +Cc: Ross Johnson, pthreads-win32, Pat Bacon

I didn't write specific test code.

The second argument was a function declared in MY-DLL that called free().

Aurelio
-----Original Message-----
From: mmh [mailto:hanasim@yahoo.com]
Sent: Thursday, February 07, 2002 11:20 AM
To: Aurelio Medina
Cc: Ross Johnson; pthreads-win32@sources.redhat.com; Pat Bacon
Subject: Re: Problem using PThreads TLS from a DLL.


    If you have written the test code which you explained, it would be 
great if you could post the code.

    I was just wondering what was specified as the second argument to 
function pthread_key_create(). Was it a function in MY-DLL or just free()?

                MMH

>I have found the real problem and a resolution for this problem.  I will summarize 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/PTHREAD-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_key_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 address 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 which tries to delete invalid memory.  MY-DLL unloads before PTHREADS-DLL because 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:
>
>>Hello all,
>>
>>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
>
>>Thanks,
>>Aurelio Medina
>>
>




_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem using PThreads TLS from a DLL.
  2002-02-06  8:52 Aurelio Medina
@ 2002-02-06 19:06 ` Ross Johnson
  0 siblings, 0 replies; 5+ messages in thread
From: Ross Johnson @ 2002-02-06 19:06 UTC (permalink / raw)
  To: Aurelio Medina; +Cc: pthreads-win32, Pat Bacon

Aurelio Medina wrote:
> 
> Hello all,
> 
> 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

> 
> Thanks,
> Aurelio Medina

-- 
+-------------------------+---+
| 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:  
http://public.ise.canberra.edu.au/~rpj/
| AUSTRALIA                   |
+-----------------------------+

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Problem using PThreads TLS from a DLL.
@ 2002-02-06  8:52 Aurelio Medina
  2002-02-06 19:06 ` Ross Johnson
  0 siblings, 1 reply; 5+ messages in thread
From: Aurelio Medina @ 2002-02-06  8:52 UTC (permalink / raw)
  To: pthreads-win32; +Cc: Pat Bacon

Hello all,

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?

Thanks,
Aurelio Medina

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-02-07 18:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-07  8:14 Problem using PThreads TLS from a DLL Aurelio Medina
2002-02-07  9:06 ` mmh
  -- strict thread matches above, loose matches on Subject: below --
2002-02-07 10:27 Aurelio Medina
2002-02-06  8:52 Aurelio Medina
2002-02-06 19:06 ` Ross Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).