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 6033671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:31:03+00:00 2026-05-23T05:31:03+00:00

I am developing an objective C framework which will ship as a static library

  • 0

I am developing an objective C framework which will ship as a static library at the end. But when I integrate that library to an actual application (by adding the static library) in the leaks tools I see some memory leaks present.

Here is an example scenario.

@implementation Test

@synthesize testNumber

+(Test) createTestInstance {

    Test *test = [[Test alloc] init];
    test.testNumber = [[NSDecimerNumber alloc] initWithInt:1];

    return test;
}

-(void) dealloc {
    [testNumber release];
}

@end

Although I release testNumber variable in dealloc I see a memory leak in Leaks tool at alloc position. What can be the issue here?

Also as this is a library provided for user to invoke, is it a best practice to release those variable from the library code?

Thank You

  • 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-05-23T05:31:03+00:00Added an answer on May 23, 2026 at 5:31 am

    I see two problems here. If testNumber is a retain property, you are overretaining it with this statement:

    test.testNumber = [[NSDecimerNumber alloc] initWithInt:1];
    

    Both alloc-init and the property accessor are retaining the object. Therefore, it should be:

    test.testNumber = [[[NSDecimerNumber alloc] initWithInt:1] autorelease];
    

    There is no need to mention that you still need to release testNumber in the dealloc method.

    Also, I understand createTestInstance is a convenience constructor to create Testobjects and it should return an autoreleased object according to the Object Ownership Policy (only methods with names that start with “alloc”, “new”, “copy”, or “mutableCopy” return an object you own):

    + (id)createTestInstance {
    
        Test *test = [[[self alloc] init] autorelease];
        test.testNumber = [[[NSDecimerNumber alloc] initWithInt:1] autorelease];
    
        return test;
    }
    

    Finally, as suggested by @Josh Caswell, convenience constructors should return id instead of the specific class. From The Objective-C Programming Language:

    The return type of convenience
    constructors is id for the same reason
    it is id for initializer methods, as
    discussed in “Constraints and Conventions.”

    Also, they should use self instead of the hard-coded class name to alloc-init the instance in order to handle subclassing properly (self here refers to the class object itself since this is a class method).

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

Sidebar

Related Questions

I'm developing a Cocoa Objective-C application that will run on Mac OS X. I
i'm developing an app in objective-c for iphone. My problem is that my application
I have a 64-bit-only application that I am developing in Objective-C on Cocoa. It
I'm developing a Cocoa/Objective C application that reads the active document from any application
I am learning Objective-C, I will be developing a video processing application. I am
I have been developing with objective C and the Cocoa framework for quite some
I am developing an Objective-C application, and what I want to do, is something
When developing iPhone apps with Xcode 3.2.1/Objective C, which unit test tools are recommended?
I'm fairly new to Cocoa and Objective-C. Currently I'm developing a fairly basic application
I am developing an application in which i need to find the apps which

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.