This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH RFA: Fix for PR libstdc++/13583


This patch is a proposed fix for PR libstdc++/13583.  That PR
complains that if two or more threads try to simultaneously create a
locale cache, there may be a memory leak.

I can't claim to fully understand all the libstdc++ ABI issues.  This
patch looks more or less correct to me, but please do take a close
look at it.

Tested by running the libstdc++ testsuite on i686-pc-linux-gnu.

OK for mainline?  Should it go into the 3.4 or 4.0 branches?

Ian


2005-09-25  Ian Lance Taylor  <ian@airs.com>

	PR libstdc++/13583
	* include/bits/locale_facets.tcc: Include "concurrence.h".
	(__gnu_internal::local_cache_mutex): New.
	(struct __use_cache<__numpunct_cache<_CharT> >): Use it when
	initializing the cache.
	(struct __use_cache<__moneypunct_cache<_CharT, _Intl> >):
	Likewise.


Index: include/bits/locale_facets.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/locale_facets.tcc,v
retrieving revision 1.218
diff -p -u -r1.218 locale_facets.tcc
--- include/bits/locale_facets.tcc	17 Aug 2005 02:12:54 -0000	1.218
+++ include/bits/locale_facets.tcc	25 Sep 2005 09:26:45 -0000
@@ -41,6 +41,12 @@
 #include <limits>		// For numeric_limits
 #include <typeinfo>		// For bad_cast.
 #include <bits/streambuf_iterator.h>
+#include <bits/concurrence.h>
+
+namespace __gnu_internal
+{
+  static __glibcxx_mutex_define_initialized(locale_cache_mutex);
+}
 
 namespace std
 {
@@ -136,18 +142,22 @@ namespace std
 	const locale::facet** __caches = __loc._M_impl->_M_caches;
 	if (!__caches[__i])
 	  {
-	    __numpunct_cache<_CharT>* __tmp = NULL;
-	    try
-	      {
-		__tmp = new __numpunct_cache<_CharT>;
-		__tmp->_M_cache(__loc);
-	      }
-	    catch(...)
+	    __gnu_cxx::lock sentry(__gnu_internal::locale_cache_mutex);
+	    if (!__caches[__i])
 	      {
-		delete __tmp;
-		__throw_exception_again;
+		__numpunct_cache<_CharT>* __tmp = NULL;
+		try
+		  {
+		    __tmp = new __numpunct_cache<_CharT>;
+		    __tmp->_M_cache(__loc);
+		  }
+		catch(...)
+		  {
+		    delete __tmp;
+		    __throw_exception_again;
+		  }
+		__loc._M_impl->_M_install_cache(__tmp, __i);
 	      }
-	    __loc._M_impl->_M_install_cache(__tmp, __i);
 	  }
 	return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
       }
@@ -163,18 +173,22 @@ namespace std
 	const locale::facet** __caches = __loc._M_impl->_M_caches;
 	if (!__caches[__i])
 	  {
-	    __moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
-	    try
-	      {
-		__tmp = new __moneypunct_cache<_CharT, _Intl>;
-		__tmp->_M_cache(__loc);
-	      }
-	    catch(...)
+	    __gnu_cxx::lock sentry(__gnu_internal::locale_cache_mutex);
+	    if (!__caches[__i])
 	      {
-		delete __tmp;
-		__throw_exception_again;
+		__moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
+		try
+		  {
+		    __tmp = new __moneypunct_cache<_CharT, _Intl>;
+		    __tmp->_M_cache(__loc);
+		  }
+		catch(...)
+		  {
+		    delete __tmp;
+		    __throw_exception_again;
+		  }
+		__loc._M_impl->_M_install_cache(__tmp, __i);
 	      }
-	    __loc._M_impl->_M_install_cache(__tmp, __i);
 	  }
 	return static_cast<
 	  const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]