Permalink
Browse files

Bug 658995 part 2 - Use static destructors instead of atexit(). r=bsm…

…edberg
  • Loading branch information...
1 parent 0b9509a commit 91e9cb183e36fc194538b445e1778f236367e7fb @glandium glandium committed Jun 15, 2011
@@ -325,7 +325,12 @@ nsStandardURL::~nsStandardURL()
}
#ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
-static void DumpLeakedURLs()
+struct DumpLeakedURLs {
+ DumpLeakedURLs() {}
+ ~DumpLeakedURLs();
+};
+
+DumpLeakedURLs::~DumpLeakedURLs()
{
if (!PR_CLIST_IS_EMPTY(&gAllURLs)) {
printf("Leaked URLs:\n");
@@ -363,8 +368,12 @@ nsStandardURL::ShutdownGlobalObjects()
NS_IF_RELEASE(gCharsetMgr);
#ifdef DEBUG_DUMP_URLS_AT_SHUTDOWN
- if (gInitialized)
- atexit(DumpLeakedURLs);
+ if (gInitialized) {
+ // This instanciates a dummy class, and will trigger the class
+ // destructor when libxul is unloaded. This is equivalent to atexit(),
+ // but gracefully handles dlclose().
+ static DumpLeakedURLs d;
+ }
#endif
}
@@ -390,7 +390,10 @@ nsresult nsProfileLock::LockWithSymlink(const nsACString& lockFilePath, PRBool a
if (!setupPidLockCleanup++)
{
// Clean up on normal termination.
- atexit(RemovePidLockFilesExiting);
+ // This instanciates a dummy class, and will trigger the class
+ // destructor when libxul is unloaded. This is equivalent to atexit(),
+ // but gracefully handles dlclose().
+ static RemovePidLockFilesExiting r;
// Clean up on abnormal termination, using POSIX sigaction.
// Don't arm a handler if the signal is being ignored, e.g.,
@@ -99,14 +99,12 @@ class nsProfileLock
LHANDLE mLockFileHandle;
#elif defined (XP_UNIX)
- static void RemovePidLockFilesExiting()
- {
- // We can't implement this function with a default parameter on
- // RemovePidLockFiles(aFatalSignal) since we register
- // atexit(RemovePidLockFilesExiting).
-
- RemovePidLockFiles(PR_FALSE);
- }
+ struct RemovePidLockFilesExiting {
+ RemovePidLockFilesExiting() {}
+ ~RemovePidLockFilesExiting() {
+ RemovePidLockFiles(PR_FALSE);
+ }
+ };
static void RemovePidLockFiles(PRBool aFatalSignal);
static void FatalSignalHandler(int signo
@@ -1365,7 +1365,7 @@ NS_TraceMallocStartup(int logfd)
log_header(logfd);
}
- atexit(NS_TraceMallocShutdown);
+ RegisterTraceMallocShutdown();
tmlock = PR_NewLock();
(void) tm_get_thread(); /* ensure index initialization while it's easy */
@@ -54,6 +54,22 @@
extern "C" const char* nsGetTypeName(void* ptr);
+extern "C" void NS_TraceMallocShutdown();
+
+struct TraceMallocShutdown {
+ TraceMallocShutdown() {}
+ ~TraceMallocShutdown() {
+ NS_TraceMallocShutdown();
+ }
+};
+
+extern "C" void RegisterTraceMallocShutdown() {
+ // This instanciates the dummy class above, and will trigger the class
+ // destructor when libxul is unloaded. This is equivalent to atexit(),
+ // but gracefully handles dlclose().
+ static TraceMallocShutdown t;
+}
+
class IUnknown {
public:
virtual long QueryInterface() = 0;

0 comments on commit 91e9cb1

Please sign in to comment.