Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with allocating memory for an Objective-C data object
    text
    copied!<p>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:</p> <pre><code>+ (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]; </code></pre> <p>}</p> <p>This method is called by another method in my view controller. This method is as follows:</p> <pre><code>+ (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], &amp;database) == SQLITE_OK) { const char *sqlStatement = [sqlString UTF8String]; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &amp;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; </code></pre> <p>}</p> <p>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.</p> <p>Please let me know if you need any clarification or any other parts of the code. Thank you in advance for your help.</p> <p>UPDATE: 4/14/2011</p> <p>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:</p> <pre><code>- (void)setUpNavigationButtons { // get array of features from feature data controller object NSArray *featureArray = [FeatureController createFeatureArray]; int i = 0; for (i = 0; i &lt; [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]; } </code></pre> <p>}</p> <p>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.</p> <p>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</p> <pre><code>@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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload