How can i release the entries object - iphone

- (id)init {
if (self == [super init]) {
entries = [[NSMutableArray alloc] init];
NSString *file = [[NSBundle mainBundle] pathForResource:#"qanda" ofType:#"db"];
sqlite3 *database = NULL;
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
sqlite3_exec(database, "select * from qanda order by question", LoadEntriesCallback, entries, NULL);
}
sqlite3_close(database);
}
return self;
}
leak near entries object: instance variable used while 'self' is not set to the result of [ super of self[init ...]]
leak near self: returning while 'self' is not set to the result of [ super of self[init ...]]

You should have
self = [super init];
not
self == [super init];
The most common pattern to use is:
self = [super init];
if (self) {
// Initialisation code
}
return self;
In your example you are not assigning the result of [super init] to anything and it is left hanging, hence the leak and the message.
If you want to release entries as per the question in you title:
entries = [[[NSMutableArray alloc] init] autorelease];
or
entries = [[NSMutableArray alloc] init];
and
[entries release];
Before the return or in your dealloc.

You can release entries by replacing :
entries = [[NSMutableArray alloc] init];
with :
entries = [[[NSMutableArray alloc] init] autorelease];
But i think the question should be : is this a good way of getting the contents from the db and frankly i think your code would be better if you moved the reading content from the db part to a loadData method.

You should autorelease it while creating.
entries = [[[NSMutableArray alloc] init] autorelease];

Try replacing
entries = [[NSMutableArray alloc] init];
with
self.entries = [[[NSMutableArray alloc] init]autorelease];
and replacing
sqlite3_exec(database, "select * from qanda order by question", LoadEntriesCallback, entries, NULL);
with
sqlite3_exec(database, "select * from qanda order by question", LoadEntriesCallback, self.entries, NULL);

Related

How and where to allocate memory?

I narrowed down my problem to not allocating memory properly. I have a NSMutableArray called subjects as a member of a Category. When I populate the category with the array of subjects it doesn't store.
I am not sure how and where to allocate the memory for subjects, which is what I believe my problem is.
Here is the snippet:
Category *cat = [[Category alloc] init];
cat.category_title = [result_categories stringForColumn:#"CATEGORY"];
QuotesAppDelegate *appDelegate = (QuotesAppDelegate *)[[UIApplication sharedApplication] delegate];
for (Subject *sb in appDelegate.subjects){
if([cat.category_title isEqualToString:sb.category_title]){
[cat.subjects addObject:sb];
NSLog(#"Adding subject: %# cat.subjects.count=%i", sb.title, cat.subjects.count);
}
}
(The above log does print cat.subject names but it doesn't print the subjects.count)
Have you allocate memory to cat.subjects like
cat.subject = [[NSMutableArray alloc] init];
or
subject = [[NSMutableArray alloc] init];
in your Category class ? if not then do it. This might be the one of the problem.
Update
first of all change your code from Category *cat = [Category alloc]; to Category *cat = [[Category alloc] init];
and change your init as
- (id)init {
self = [super init];
if (self) {
subjects = [[NSMutableArray alloc] init];
}
return self;
}

Sectioned tableview problem

I have a database app i am making and i can extract the results from a sqlite database and put them in the table, however i need to make them sort alphabetically in sections.
So now i have this code
AArray = [[NSMutableArray alloc] init];
BArray = [[NSMutableArray alloc] init];
CArray = [[NSMutableArray alloc] init];
DArray = [[NSMutableArray alloc] init];
EArray = [[NSMutableArray alloc] init];
FArray = [[NSMutableArray alloc] init];
GArray = [[NSMutableArray alloc] init];
HArray = [[NSMutableArray alloc] init];
IArray = [[NSMutableArray alloc] init];
JArray = [[NSMutableArray alloc] init];
KArray = [[NSMutableArray alloc] init];
LArray = [[NSMutableArray alloc] init];
MArray = [[NSMutableArray alloc] init];
NArray = [[NSMutableArray alloc] init];
OArray = [[NSMutableArray alloc] init];
PArray = [[NSMutableArray alloc] init];
QArray = [[NSMutableArray alloc] init];
RArray = [[NSMutableArray alloc] init];
SArray = [[NSMutableArray alloc] init];
TArray = [[NSMutableArray alloc] init];
UArray = [[NSMutableArray alloc] init];
VArray = [[NSMutableArray alloc] init];
WArray = [[NSMutableArray alloc] init];
XArray = [[NSMutableArray alloc] init];
YArray = [[NSMutableArray alloc] init];
ZArray = [[NSMutableArray alloc] init];
and i have code to move each item from the database into the relevant array, this all works fine.
I then have this code:
All = [NSMutableDictionary dictionaryWithObjectsAndKeys:AArray,#"A",BArray,#"B",CArray,#"C",DArray,#"D",EArray,#"E",FArray,#"F",GArray,#"G",HArray,#"H",IArray,#"I",JArray,#"J",KArray,#"K",LArray,#"L",MArray,#"M",NArray,#"N",OArray,#"O",PArray,#"P",QArray,#"Q",RArray,#"R",SArray,#"S",TArray,#"T",UArray,#"U",VArray,#"V",WArray,#"W",XArray,#"X",YArray,#"Y",ZArray,#"Z",nil];
AllArray = [NSArray alloc];
AllArray = [AllArray initWithArray:[[All allKeys]sortedArrayUsingSelector:#selector(compare:)]];
and this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [AllArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *Array = [All objectForKey:[AllArray objectAtIndex:section]];
return [Array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
int sectionindex = section;
return [AllArray objectAtIndex:sectionindex];
}
When i run it works fine, however if i scroll up and down a few times the app crashes without any error message.
It is something to do with the sections as it crashes when i add them in, but i just cannot see what im doing wrong
Everythings declared in the h file and i #property and #synthesize the following
#property (nonatomic,retain) NSMutableDictionary *All;
#property (nonatomic,retain) NSArray *AllArray;
#property (nonatomic, retain) UITableView *TableView;
If anyone could help me it would be really great!
Thanks!
Your dictionary "All" does not seem to be retained. You appear to assign the dictionary object directly to the instance variable, not to the property (in which case you would have used self.All).
If your app crashes without a message, then make sure to enable the Breakpoints button in Xcode's toolbar. This will run the app in the debugger, which will give you more helpful information about the crash. Setting NSZombieEnabled to YES also helps.
Take a look here:
Documentation
The simplest approach is, to provide a sort selector (see the link for details):
sortedArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];

Autoreleased NSMutableArray not populated

I want to populate an array like this:
NSMutableArray *array = [self methodThatReturnsAnArray];
In the "methodThatReturnsAnArray"-method I create an array like this:
NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];
When I'm finished populating "arrayInMethod" I'm returning the array and in order to prevent a memory leak I'm using:
return [arrayInMethod autorelease];
However the "array"-variable is never populated. When removing the "autorelease" it works fine though. What should I do in order to make sure that the returned object i released?
EDIT
+ (NSMutableArray *)buildInstants:(NSArray *)huntsArray {
NSMutableArray *goGetObjects = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < [huntsArray count]; i++) {
NSDictionary *huntDict = [huntsArray objectAtIndex:i];
PHGoGet *goGet = [[PHGoGet alloc] init];
goGet.title = [huntDict objectForKey:#"title"];
goGet.description = [huntDict objectForKey:#"description"];
goGet.start = [huntDict objectForKey:#"start"];
goGet.end = [huntDict objectForKey:#"end"];
goGet.ident = [huntDict objectForKey:#"id"];
if ((CFNullRef)[huntDict objectForKey:#"image_url"] != kCFNull) {
goGet.imageURL = [huntDict objectForKey:#"image_url"];
} else {
goGet.imageURL = nil;
}
if ((CFNullRef)[huntDict objectForKey:#"icon_url"] != kCFNull) {
goGet.iconURL = [huntDict objectForKey:#"icon_url"];
} else {
goGet.iconURL = nil;
}
goGet.longitude = [huntDict objectForKey:#"lng"];
goGet.latitude = [huntDict objectForKey:#"lat"];
goGet.companyIdent = [huntDict objectForKey:#"company_id"];
[goGetObjects insertObject:goGet atIndex:i];
[goGet release];
}
return [[goGetObjects copy] autorelease];
}
Try using the convienence method for NSMutableArray... change:
NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];
To...
NSMutableArray *arrayInMethod = [NSMutableArray array];
array will return an autoreleased object.
First of all, I recommend you not to return a NSMutableArray from any method. It's better to use NSArray for that to avoid some very difficult to debug problems. My proposition is:
You declare the mutable array and populate it:
NSMutableArray *arrayInMethod = [[[NSMutableArray alloc] init] autorelease];
Then you return an autoreleased copy:
return [[arrayInMethod copy] autorelease];
And finally, when you take the returned array, you make it mutable again (only if you need to change it):
NSMutableArray *array = [[self methodThatReturnsAnArray] mutableCopy];
When you're done with the array, you release it:
[array release];

Problem with a NSMutableArray Leak

I thought I knew how to deal with memory leaks and arrays, but then this pops up. I can't figure pout why this is leaking:
// MyViewController.h
NSMutableArray *myMutableArray;
#property (nonatomic, retain) NSMutableArray *myMutableArray;
// MyViewController.m
#synthesize myMutableArray;
- (void) viewDidLoad {
if (self.myMutableArray == nil) {
self.myMutableArray = [[NSMutableArray alloc] init];
}
. . .
for (NSUInteger i = 0; i < someCount; ++i) {
[self.myMutableArray addObject:[NSString stringWithFormat: #"%#",myString]];
}
}
- (void)viewDidUnload {
self.myMutableArray = nil
}
- (void)dealloc {
[myMutableArray release];
}
Your problem is here:
if (self.myMutableArray == nil) {
self.myMutableArray = [[NSMutableArray alloc] init];
}
It should be:
if (myMutableArray == nil) {
self.myMutableArray = [[[NSMutableArray alloc] init] autorelease];
}
Or:
if (myMutableArray == nil) {
myMutableArray = [[NSMutableArray alloc] init];
}
Explanation:
Since you are using retain as a property mutator attribute, the object will be retained when it is passed to the property setter, therefore you have a leak when you retain an object you already have ownership of.
The solution to this is to either a) Pass an autorelease-d object to the property setter or b) Assign the ivar directly to the alloc-ed object.
You're allocating a new array, then setting it to a retain property. Change that line to
self.myMutableArray = [NSMutableArray array];
self.myMutableArray = [[NSMutableArray alloc] init];
should be
self.myMutableArray = [[[NSMutableArray alloc] init] autorelease];
because myMutableArray is a retained property.

Initializing a class. Can you see any problems with this?

-(id)init {
if (self = [super init]) {
self.name = [[NSString alloc] init];
self.type = [[NSString alloc] init];
self.phoneNumber = [[NSString alloc]init];
self.webAddress = [[NSString alloc] init];
NSMutableArray *pricesArray = [[NSMutableArray alloc] init];
NSMutableArray *poolsArray = [[NSMutableArray alloc] init];
self.prices = pricesArray;
self.pools = poolsArray;
[pricesArray release];
[poolsArray release];
//Create the address dictionaries
NSMutableDictionary *addressItems = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"", KAddressStreet1Key, #"", KAddressStreet2Key, #"", KAddressBoroughKey, #"", KAddressCityKey, #"", KAddressCountyKey, #"", KAddressPostCodeKey, #"" ,KAddressCountryKey, nil];
//Add dictionary to the array to contain address values
self.address = addressItems;
[addressItems release];
}
return self;
}
I'm currently doing a massive round of debugging thanks to EXC_BAD_ACCESS errors.. grr.
Does the code above seem fine and logical for a class init method? Basically I'm getting the EXC_BAD_ACCESS errors when I release both the pools (mutable array and the dictionary).
How are your properties declared? If they are not declared with retain then most of your objects will be deallocated at the end of this method.
You're leaking objects with each allocation for the string properties. Other than that, I don't notice anything wrong. How are the AddressXKeys defined?