Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:05:01+00:00 2026-06-03T22:05:01+00:00

I have a method that will calculate the height of a text line. But

  • 0

I have a method that will calculate the height of a text line. But the problem is it leak the memory with 2 items: CTFrame and a Malloc (48 byte).

Here are the core of that method:

(void)calculatePageHeight {
    __weak UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];

    NSString *sampleText = @"The CTFont opaque type represents a Core Text font object. Font objects represent fonts to an application, providing access to characteristics of the font, such as point size, transform matrix, and other attributes. Fonts provide assistance in laying out glyphs relative to one another and are used to establish the current font when drawing in a graphics context.";
    NSRange contentRange = NSRangeFromString(sampleText);
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:sampleText attributes:self.textAttributes];

    CFAttributedStringRef attributeRef = (__bridge CFAttributedStringRef)attributedString;

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributeRef);

    CGPathRef myPath = [path CGPath];
    CFRange myRange = CFRangeMake(contentRange.location, contentRange.length);

    CTFrameRef contentFrame = CTFramesetterCreateFrame(framesetter, myRange , myPath, nil);
    CFArrayRef lines = CTFrameGetLines(CFRetain(contentFrame));
    NSInteger lineCount = CFArrayGetCount(lines);
    CGPoint *origins = calloc(lineCount, sizeof(CGPoint));

    CTFrameGetLineOrigins(contentFrame, CFRangeMake(0, 0), origins);
    CGFloat lineSpacing = 0;

    for (NSInteger index = 0; index < lineCount; index ++) {
        CTLineRef line = CFArrayGetValueAtIndex(lines, index);
        CGFloat ascent;
        CGFloat descent;

        CTLineGetTypographicBounds(line, &ascent, &descent, nil);
        NSLog(@"line height: %f", ascent + (descent * 2));

        lineSpacing = ascent + (descent * 2);
    }
    free(origins);
    CFRelease(lines);
    //free(contentFrame);

    NSLog(@"line spacing: %f", lineSpacing);

    NSInteger numberOfLine = TEXT_PAGE_HEIGHT / (lineSpacing);

    CGFloat pageHeight = numberOfLine * (lineSpacing);
    self.pageHeight = pageHeight;

    CGPathRelease(myPath);
    CFRelease(framesetter);
}

If I uncomment the line, the CTFrame will be out, but there will be a warning:

Passing CTFrameRef (aka const struct_CTFrame *) to parameter of type "void *' discards qualifiers)

free(contentFrame);

then the leaking will have only one for Malloc.
The instrument tool let me know this line of code cause the leaking.

CTFrameRef contentFrame = CTFramesetterCreateFrame(framesetter, myRange , myPath, nil);

Any one can help me to explain this, I can not explain why the Malloc is leaking. And how to fix that, how to release CTFrame object as well ?

I research so much but could not found a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:05:02+00:00Added an answer on June 3, 2026 at 10:05 pm

    Use CFRelease instead of free, the documentation doesn’t mention using CFRelease to deallocate CT* objects, but it just works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that will receive a string , but before I can
I have created a custom method that will return unique items, together with the
I have a method with this signature, and another method that will be the
I have a method that I will use in the following contexts: 1. User
I have some code in method C that will get executed based upon the
I am trying to have a method that takes in a username and will
I'm creating a class that will have one public method, which returns a value
I am writing a simple method that will calculate the number of decimal places
I have a method that will provide an array of model object. Some of
I have a method that receives a number n as parameter and calculate all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.