kostub / iosMath

Beautiful math equation rendering on iOS and MacOS
MIT License
1.36k stars 234 forks source link

Fix memory leak in MTFont #175

Closed maitbayev closed 1 year ago

maitbayev commented 1 year ago

MTFont doesn't handle memory management correctly for CTFontRef and CGFontRef. The ownership policy for Core Foundation as following:

Foundation:

  • If you create an object (either directly or by making a copy of another object—see The Create Rule), you own it.
  • If you get an object from somewhere else, you do not own it. If you want to prevent it being disposed of, you must add yourself as an owner (using CFRetain).
  • If you are an owner of an object, you must relinquish ownership when you have finished using it (using CFRelease).

The Create Rule:

Core Foundation functions have names that indicate when you own a returned object:

  • Object-creation functions that have “Create” embedded in the name
  • Object-duplication functions that have “Copy” embedded in the name

Per the above policy, MTFont shouldn't call CFRetain after CTFontCreateWithGraphicsFont or CGFontCreateWithDataProvider since CFRetain is already invoked when creating the objects.

maitbayev commented 1 year ago

@kostub PTAL