Better lock protection of shared instance and cleanup at exit

This commit is contained in:
rfm 2025-03-27 09:57:28 +00:00
parent ce9699d5c5
commit 9fadc19f8c

View file

@ -661,7 +661,9 @@ newLanguages(NSArray *oldNames)
+ (void) atExit + (void) atExit
{ {
[classLock lock];
DESTROY(sharedDefaults); DESTROY(sharedDefaults);
[classLock unlock];
DESTROY(bundleIdentifier); DESTROY(bundleIdentifier);
DESTROY(processName); DESTROY(processName);
DESTROY(argumentsDictionary); DESTROY(argumentsDictionary);
@ -842,11 +844,13 @@ newLanguages(NSArray *oldNames)
NS_ENDHANDLER NS_ENDHANDLER
if (nil != regDefs) if (nil != regDefs)
{ {
[self standardUserDefaults]; NSUserDefaults *defs = [self standardUserDefaults];
if (sharedDefaults != nil)
if (defs != nil)
{ {
[sharedDefaults->_tempDomains setObject: regDefs [defs->_lock lock];
forKey: NSRegistrationDomain]; [defs->_tempDomains setObject: regDefs forKey: NSRegistrationDomain];
[defs->_lock unlock];
} }
} }
} }
@ -1353,6 +1357,21 @@ newLanguages(NSArray *oldNames)
return [self initWithContentsOfFile: path]; return [self initWithContentsOfFile: path];
} }
/* This code ensures that, if the shared instance is about to be deallocated
* we clear the sharedDefaults variable first so that no other thread can
* grab a reference to the deallocated instance.
*/
- (void) release
{
[classLock lock];
if (self == sharedDefaults && [self retainCount] < 2)
{
sharedDefaults = nil;
}
[classLock unlock];
[super release];
}
- (void) dealloc - (void) dealloc
{ {
[[NSNotificationCenter defaultCenter] removeObserver: self]; [[NSNotificationCenter defaultCenter] removeObserver: self];