app_server: FontStyle lifecycle

FontFamily is babysitted by the FontManager. It doesn't own FontStyle
references, there's no place in it to release them.

FontManager owns a reference to the FontStyle, and it is in
fStyleHashTable. Putting a style there acquires a new reference, so we
can release the one from the new style. Removing a style from the map
releases the reference. We need to clear the map before shutting down
FreeType to get rid of our last references and let the styles die now
instead of afterwards to avoid double freeing the faces.

These solve the new crash from the previous patch. It didn't crash
before because even after the map was destroyed there were still
dangling references to the styles.

On removing a user font, the style will remove itself from the family
through the manager if that was the last reference, and the manager will
remove and delete the family if it has no more styles.

Change-Id: I460ff830fa8a8a5adb90dc8ea12120e1e50a5912
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6052
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Máximo Castañeda 2023-02-08 12:27:04 +01:00 committed by waddlesplash
parent 6d024fde1c
commit a34c877fd0
5 changed files with 11 additions and 26 deletions

View File

@ -1766,12 +1766,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
status_t status = B_OK; status_t status = B_OK;
AutoLocker<FontManagerBase> fontLock(fAppFontManager); AutoLocker<FontManagerBase> fontLock(fAppFontManager);
FontStyle* style = fAppFontManager->GetStyle(familyID, styleID); status = fAppFontManager->RemoveUserFont(familyID, styleID);
if (style != NULL) {
status = fAppFontManager->RemoveUserFont(familyID, styleID);
} else
status = B_BAD_VALUE;
fLink.StartMessage(status); fLink.StartMessage(status);
fLink.Flush(); fLink.Flush();

View File

@ -95,6 +95,7 @@ AppFontManager::_AddUserFont(FT_Face face, node_ref nodeRef, const char* path,
styleID = style->ID(); styleID = style->ID();
fStyleHashTable.Put(FontKey(familyID, styleID), style); fStyleHashTable.Put(FontKey(familyID, styleID), style);
style->ReleaseReference();
return B_OK; return B_OK;
} }
@ -160,19 +161,7 @@ AppFontManager::RemoveUserFont(uint16 familyID, uint16 styleID)
ASSERT(IsLocked()); ASSERT(IsLocked());
FontKey fKey(familyID, styleID); FontKey fKey(familyID, styleID);
FontStyle* styleRef = fStyleHashTable.Get(fKey); FontStyle* styleRef = fStyleHashTable.Remove(fKey);
fStyleHashTable.Remove(fKey);
FontFamily* family = styleRef->Family(); return styleRef != NULL ? B_OK : B_BAD_VALUE;
bool removed = family->RemoveStyle(styleRef, this);
if (!removed)
syslog(LOG_DEBUG, "AppFontManager::RemoveUserFont style not removed from family\n");
fFamilies.RemoveItem(family);
delete family;
styleRef->ReleaseReference();
return B_OK;
} }

View File

@ -78,8 +78,6 @@ FontFamily::~FontFamily()
// we remove us before deleting the style, so that the font manager // we remove us before deleting the style, so that the font manager
// is not contacted to remove the style from us // is not contacted to remove the style from us
style->_SetFontFamily(NULL, -1); style->_SetFontFamily(NULL, -1);
style->ReleaseReference();
} }
} }

View File

@ -69,6 +69,8 @@ FontManagerBase::~FontManagerBase()
for (int32 i = fFamilies.CountItems(); i-- > 0;) for (int32 i = fFamilies.CountItems(); i-- > 0;)
delete fFamilies.ItemAt(i); delete fFamilies.ItemAt(i);
fStyleHashTable.Clear();
if (fHasFreetypeLibrary == true) if (fHasFreetypeLibrary == true)
FT_Done_FreeType(gFreeTypeLibrary); FT_Done_FreeType(gFreeTypeLibrary);
} }
@ -306,8 +308,10 @@ FontManagerBase::RemoveStyle(FontStyle* style)
debugger("style removed but still available!"); debugger("style removed but still available!");
if (family->RemoveStyle(style) if (family->RemoveStyle(style)
&& family->CountStyles() == 0) && family->CountStyles() == 0) {
fFamilies.RemoveItem(family); fFamilies.RemoveItem(family);
delete family;
}
} }
@ -320,4 +324,4 @@ FontManagerBase::_FindFamily(const char* name) const
FontFamily family(name, 0); FontFamily family(name, 0);
return const_cast<FontFamily*>(fFamilies.BinarySearch(family, return const_cast<FontFamily*>(fFamilies.BinarySearch(family,
compare_font_families)); compare_font_families));
} }

View File

@ -484,8 +484,6 @@ GlobalFontManager::_RemoveStyle(font_directory& directory, FontStyle* style)
directory.revision++; directory.revision++;
fStyleHashTable.Remove(FontKey(style->Family()->ID(), style->ID())); fStyleHashTable.Remove(FontKey(style->Family()->ID(), style->ID()));
style->ReleaseReference();
} }
@ -741,6 +739,7 @@ GlobalFontManager::_AddFont(font_directory& directory, BEntry& entry)
directory.styles.AddItem(style); directory.styles.AddItem(style);
fStyleHashTable.Put(FontKey(style->Family()->ID(), style->ID()), style); fStyleHashTable.Put(FontKey(style->Family()->ID(), style->ID()), style);
style->ReleaseReference();
if (directory.AlreadyScanned()) if (directory.AlreadyScanned())
directory.revision++; directory.revision++;