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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:40:00+00:00 2026-05-21T08:40:00+00:00

I’ve been programming objective-C for a few months now and have done pretty well

  • 0

I’ve been programming objective-C for a few months now and have done pretty well so far without having to post any questions. This would be my first. The problem is that I’m getting a memory leak warning from a data object in one of it’s methods. I can see that the problem is that I’m sending an alloc to it without releasing it, but I don’t know how else to get it to retain the object in memory. If I take the alloc out, the program crashes. If I leave it in, it leaks memory. Here is the method in question:

+ (id) featureWithID:(int)fID name:(NSString*)fName secure:(int)fSecure {
Feature *newFeature = [[self alloc] init];
newFeature.featureID = fID;
newFeature.featureName = fName;
newFeature.featureSecure = fSecure;

return [newFeature autorelease];

}

This method is called by another method in my view controller. This method is as follows:

+ (NSMutableArray*) createFeatureArray {

NSString *sqlString = @"select id, name, secure from features";
NSString *file = [[NSBundle mainBundle] pathForResource:@"productname" ofType:@"db"];
sqlite3 *database = NULL;
NSMutableArray *returnArray = [NSMutableArray array];

if(sqlite3_open([file UTF8String], &database) == SQLITE_OK) {

    const char *sqlStatement = [sqlString UTF8String];
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

            Feature *myFeature = [Feature featureWithID:sqlite3_column_int(compiledStatement,0) 
                                                   name:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]
                                                 secure:sqlite3_column_int(compiledStatement,2)];

            [returnArray addObject:myFeature];

        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return returnArray;

}

I have tried several things, such as creating a featureWithFeature class method, which would allow me to alloc init the feature in the calling method, but that crashed the program also.

Please let me know if you need any clarification or any other parts of the code. Thank you in advance for your help.

UPDATE: 4/14/2011

After reading the first two responses I implemented the suggestion and found that the program is now crashing. I am at a complete loss as to how to track down the culprit. Hoping this helps, I am posting the calling method from the view controller as well:

- (void)setUpNavigationButtons {
// get array of features from feature data controller object
NSArray *featureArray = [FeatureController createFeatureArray];
int i = 0;


for (i = 0; i < [featureArray count]; i++) {
    Feature *myFeature = [featureArray objectAtIndex:i];
    CGRect buttonRect = [self makeFeatureButtonFrame:[featureArray count] withMember:i];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton setFrame:buttonRect];

    [aButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [aButton setTitle:[NSString stringWithFormat:@"%@",myFeature.featureName] forState:UIControlStateNormal];
    aButton.tag = myFeature.featureID;

    [self.view addSubview:aButton];

}

}

NOTE: These methods are posted in reverse of the order they are invoked. This last method calls the second method, which in turn, calls the first.

UPDATE: I’ve updated these functions to show what is in there now: Below, I will post the header files for the object – maybe that will help

@interface Feature : NSObject {
    int         featureID;
    int         featureSecure;
    NSString    *featureName;
}

@property (nonatomic, assign) int featureID;
@property (nonatomic, assign) int featureSecure;
@property (nonatomic, retain) NSString *featureName;

- (id) init;

- (void) dealloc;

+ (id) featureWithID:(int)fID name:(NSString*)fName secure:(int)fSecure;



@end

@interface FeatureController : NSObject {

}


- (id) init;

- (void) dealloc;

+ (NSMutableArray*) createFeatureArray;

+ (Feature*) getFeatureWithID:(int)fetchID;

@end
  • 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-21T08:40:01+00:00Added an answer on May 21, 2026 at 8:40 am

    In +createFeatureArray, you’re over releasing the array:

    + (NSMutableArray*) createFeatureArray {
        …
        NSMutableArray *returnArray = [[[NSMutableArray alloc] init] autorelease];
        …
        return [returnArray autorelease];
    }
    

    In the first line, you used +alloc, so you own the array. Then you used -autorelease, so you do not own the array any more. This means that you shouldn’t send -release or -autorelease to it, which you are doing in the return line.

    You can fix that by changing those lines to:

    + (NSMutableArray*) createFeatureArray {
        …
        NSMutableArray *returnArray = [NSMutableArray array];
        …
        return returnArray;
    }
    

    Also, unless it is relevant to callers that the array is mutable, you should change that method to return NSArray instead of NSMutableArray. You could keep your code as is, i.e., return a mutable array even though the method declaration states that the return type is NSArray.


    As for your convenience constructor, there are essentially two choices depending on whether you want to return an owned or a non-owned object:

    • if you want to return an owned object, allocate it with +alloc or +new and return it without autoreleasing it. Your method name should contain new, e.g. +newFeatureWithId:…

    • if you want to return an object that’s not owned by the caller, allocate it with +alloc or new and autorelease it before/upon returning it to the caller. Your method name should not contain new, alloc, or copy.


    In -setUpNavigationButtons, you obtain a non-owned array via +createFeatureArray, allocate a mutable array based on it, and release the mutable array without adding or removing elements from it. A mutable array makes sense when you need to add/remove elements. If you don’t have this need, you could change your method to:

    - (void)setUpNavigationButtons {
    // get array of features from feature data controller object
    NSArray *featureArray = [FeatureController createFeatureArray];
    …
    // [featureArray release];
    

    You’d remove that [featureArray release] since you do not own featureArray inside that method.


    Edit: In -setUpNavigationButtons, you’re retaining the button you create and soon after you’re releasing it. In that particular method, those are idempotent operations — they aren’t wrong per se but are not necessary at all. You could replace that code with

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    …
    [self.view addSubview:aButton];
    // [aButton release];
    

    i.e., do not retain it and do not release it.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but

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.