Sample Cocoa app, ref counting, [pool drain] yields EXC_BAD_ACCESS? - objective-c

I'm working through Cocoa Programming for Mac OS X (3rd ed) and in chapter 4 I wrote this app:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//create the date object
NSCalendarDate *now = [[NSCalendarDate alloc] init];
//seed random # generator
srandom(time(NULL));
NSMutableArray *array;
array = [[NSMutableArray alloc] init];
int i;
for (i=0; i<10; i++){
//create a date/time that is 'i' weeks from now
NSCalendarDate *iWeeksFromNow;
iWeeksFromNow = [now dateByAddingYears:0
months:0
days:(i*7)
hours:0
minutes:0
seconds:0];
//create a new instance of lottery entry
LotteryEntry *entry = [[LotteryEntry alloc] init];
[entry setEntryDate:iWeeksFromNow];
[array addObject:entry];
[entry release];
}
[now release];
now = nil;
for (LotteryEntry *entryToPrint in array) {
NSLog(#"%#", entryToPrint);
}
[array release];
array = nil;
NSLog(#"about to drain the pool... (%#)", pool);
[pool drain];
NSLog(#"done");
NSLog(#"GC = %#", [NSGarbageCollector defaultCollector]);
return 0;
}
The LotteryEntry class looks like this:
#implementation LotteryEntry
- (void)setEntryDate:(NSCalendarDate *)date
{
entryDate = date;
}
- (NSCalendarDate *)entryDate
{
return entryDate;
}
- (int)firstNumber
{
return firstNumber;
}
- (int)secondNumber
{
return secondNumber;
}
- (id)init
{
return [self initWithDate:[NSCalendarDate calendarDate]];
}
- (id)initWithDate:(NSCalendarDate *)date
{
if(![super init])
return nil;
NSAssert(date != nil, #"Argument must be non-nil");
firstNumber = random() % 100 + 1;
secondNumber = random() % 100 + 1;
entryDate = [date retain];
return self;
}
- (NSString *)description
{
NSString *result;
result = [[NSString alloc] initWithFormat:#"%# = %d and %d",
[entryDate descriptionWithCalendarFormat:#"%b %d %Y"],
firstNumber,
secondNumber];
return result;
}
- (void)dealloc
{
NSLog(#"deallocating %#", self);
[entryDate release];
[super dealloc];
}
#end
As you can see I'm retaining and releasing the objects here. I'm pretty sure my code matches the book's, however when I run the app, at the [pool drain] I get this message:
Program received signal:
“EXC_BAD_ACCESS”.
I'm not sure what's causing this. I'm hoping it's something stupid that I missed, but I'd sure appreciate a few other pairs of eyes on it. Thanks in advance!
(side note: I'm a .NET developer, so ref counting is pretty foreign to me!)

It also looks like you have a bug in this method:
- (id)initWithDate:(NSCalendarDate *)date
{
if(![super init])
return nil;
NSAssert(date != nil, #"Argument must be non-nil");
firstNumber = random() % 100 + 1;
secondNumber = random() % 100 + 1;
entryDate = [date retain];
return self;
}
You are essentially discarding the results from [super init], while it may not be a problem in this instance, it could cause serious problems in others. You should 'always' structure you init methods like this:
- (id)initWithDate:(NSCalendarDate *)date
{
if(self = [super init]) {
NSAssert(date != nil, #"Argument must be non-nil");
firstNumber = random() % 100 + 1;
secondNumber = random() % 100 + 1;
entryDate = [date retain];
}
return self;
If you are not going to return self from an init method (for instance it is a factory or something odd like that), you should remember to release self. It has been alloc'ed, and if you don't return it, it cannot be released properly. Example:
- (id) init
{
NSObject* newSelf = [[NSObject alloc] init];
[self release];
return newSelf;
}

doh! Just typing the code made me realize my problem. Doncha love it when that happens?
I am retaining the date in my init, but I still had that extra setEntryDate method that was not calling retain. Removing this and calling the initWithDate method instead seemed to fix the problem.

It's not related to your issue at hand but you should avoid using NSCalendarDate, it's been deprecated for a while now and will probably be removed from the API entirely soon.

Related

How can I convince XCode's Analyzer that this isn't a leak?

The static analyzer is informing me that the following code has a potential leak. I don't understand how there's any room for a leak. Further, I don't understand how the analyzer can be so helpful across the entire project yet miss something this easy.
My assumption is that the analyzer is right and I am leaking. But how?
+ (McFieldDefinition *) freeformFieldDefinition {
return [[[McFieldDefinition alloc] initWithText:#"0201FFM100"] autorelease];
}
Thanks!
Sorry for posting this question. I finally found an answer here: https://stackoverflow.com/a/15668026/300986.
The problem was in my init method:
- (id) initWithText:(NSString *)text {
if (!text) return nil;
if ([text length] < 7) return nil;
self = [self init];
if (self) {
// do stuff
}
return self;
}
Those two guard clauses return nil if I don't like the text variable. self is already alloc'ed by that point, so it's Analyzer 1, bmauter 0.
Here's my new version:
- (id) initWithText:(NSString *)text {
self = [self init];
if (!self) return nil;
if (!text || [text length] < 7) {
[self release];
return nil;
}
// do stuff
return self;
}

NSMutableArray subclass init

I'm trying to make a function for a NSMutableArray subclass that only uses integer, but I don't want to use "count." How do I do this?
-(NSMutableArrayWithIntegers*)initWithCount:(NSInteger)count numbers:(NSInteger)firstInt, ...
{
self = [super init];
if (self) {
va_list args;
va_start(args, firstInt);
NSInteger arg = firstInt;
for (int i = 0; i < count; i++)
{
arg = va_arg(args, NSInteger);
[self addObject: [NSNumber numberWithInteger:arg]];
}
va_end(args);
}
return self;
}
I know this doesn't answer your question but it's important to let you know. Don't ever subclass NSMutableAnything. Use a category and thank me later:
#interface NSMutableArray (ListOfIntegers)
+(NSMutableArray)mutableArrayWithIntegers:(NSInteger)i, ... {
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:whatever];
// do your thing
return array;
}
#end
First of all, the approach you currently have is just fine. Don't try getting rid of the count. There are alternatives, but they are only worse.
For example, you may use a sentinel value (which may not be inserted into the array) as the last argument, but in this case, you will have to make sure that you are not actually trying to insert this value to the array at all:
- (id)initWithIntegers:(NSInteger)first, ...
{
if (!(self = [super init])) return nil;
va_list args;
va_start(args, first);
NSInteger n;
if (first != NSIntegerMax) {
[self addObject:#(first)];
while ((n = va_arg(args, NSInteger)) != NSIntegerMax) {
[self addObject:#(n)];
}
}
va_end(args);
return self;
}
But really, this unnecessarily narrows the range of values that can be added - using that count argument is not a big deal.

Need help in finding a memory leak in copying Sqlite Database to Mutable array

I seem to be having a 1.19 KB leak somewhere in the following code. opening up the call tree in instruments, I have narrowed down the leaks to the following:
61.8% of the leaks are coming from +[NSString stringWithUTF8String:]
38.1% of the leaks are coming from +[NSNumber numberWithDouble:]
-(void) readMinesFromDatabase
{
NSLog(#" Setup the database object");
sqlite3 *database;
// Init the Array
northernMines = [[NSMutableArray alloc] init];
nCentralMines = [[NSMutableArray alloc] init];
centralMines = [[NSMutableArray alloc] init];
southernMines = [[NSMutableArray alloc] init];
//NSLog(#" pre if statement");
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "SELECT * FROM mines";
//NSLog(#"pre 2nd if statement");
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) != SQLITE_OK)
{
NSLog( #"Error: Failed to prepare stmt with message %s", sqlite3_errmsg(database));
}
else
{
// Loop through the results and add them to the feeds array
NSLog(#"pre loop");
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *name;
NSString *com;
NSString *county;
NSNumber *lat;
NSNumber *longit;
// Read the data from the result row
//deals with null strings in the name and commodity fields of the database
//NSLog(#"ered the loop");
if (sqlite3_column_text(compiledStatement, 3) != NULL) {
name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
}
else {
name = #"";
}
if (sqlite3_column_text(compiledStatement, 10) != NULL) {
com = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)];
}
else {
com = #"";
}
//latitude and longitudes
lat = [NSNumber numberWithDouble:(double )sqlite3_column_double(compiledStatement, 4)];
longit = [NSNumber numberWithDouble:(double )sqlite3_column_double(compiledStatement, 5)];
//NSLog(#"long %#",longit);
// Create a new object with the data from the database
Mine *mine = [[Mine alloc] initWithMineName:name latitudeInitial:lat longitudeInitial:longit commodity:com];
// Add the object to the animals Array
county = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
if([county isEqualToString:#"Butte"] || [county isEqualToString:#"Plumas"] || [county isEqualToString:#"Yuba"] || [county isEqualToString:#"Sierra"])
{
[northernMines addObject:mine];
}
else if([county isEqualToString:#"Nevada" ]|| [county isEqualToString:#"Placer"] || [county isEqualToString:#"El Dorado"] || [county isEqualToString:#"Sutter"])
{
[nCentralMines addObject:mine];
}
else if([county isEqualToString:#"Amador"] || [county isEqualToString:#"Sacramento"] || [county isEqualToString:#"Calaveras"] || [county isEqualToString:#"San Joaquin"] || [county isEqualToString:#"Stanislaus"])
{
[centralMines addObject:mine];
}
else if([county isEqualToString:#"Tuolumne"] ||[county isEqualToString:#"Mariposa"] || [county isEqualToString:#"Madera"] || [county isEqualToString:#"Merced"])
{
[southernMines addObject:mine];
}
else
{
}
[mine release];
//[name release];
//[com release];
//[county release];
//[lat release];
//[longit release];
}
NSLog(#"done with loop");
//[mines addObject:#"nil"];
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
the mine object implementation file is:
#import "Mine.h"
#implementation Mine
#synthesize mineName, latitudeInitial, longitudeInitial, commodity;
-(id)initWithMineName:(NSString *)n latitudeInitial:(NSNumber *)l longitudeInitial:(NSNumber *)g commodity:(NSString *)c
{
self.mineName = n;
self.latitudeInitial = l;
self.longitudeInitial = g;
self.commodity = c;
return self;
}
#end
Well, it is a little hard to tell because of the indentation where the function ends (is this the entire function?) but I believe you are forgetting to release the 4 arrays that you are allocating at the beginning of the function. By leaking the arrays you are also leaking their contents.
Edit - for the initializer code you added:
I am not sure if this has anything to do with the memory leaks but I noticed that the initializer is not implemented correctly:
you should call init on super and update self (more details here)
of this I am not 100% sure, but I think you should not send messages (call methods) to the current object from inside the init method, and by using the dot notation you are actually calling the setter methods (but again, I am not really sure of this one)
I suppose that the properties are declared as (retain) and that you release them in the dealloc method. Which would be correct.
I can't really see anything else wrong with the code you showed. Maybe the problem is not exactly here (just an idea).
Edit 2 - example initializer
You should spend some time reading the Apple documentation, it has plenty of examples.
Here is my version (I hope it doesn't have too many mistakes):
-(id)initWithMineName:(NSString *)n latitudeInitial:(NSNumber *)l longitudeInitial:(NSNumber *)g commodity:(NSString *)c {
// Assign self to value returned by super's designated initializer
// Designated initializer for NSObject is init
self = [super init];
if (self) {
mineName = [n retain];
latitudeInitial = [l retain];
longitudeInitial = [g retain];
commodity = [c retain];
}
return self;
}
Just a thought, but could the memory leak be in the Mine object initializer ( -[Mine initWithMineName: latitudeInitial: longitudeInitial: commodity:] ) ??

Problem with NSMutableArray and custom NSObject

I'm really desperate on this one. I'm trying to make a Framework which you can search and play YouTube videos with. But while testing it, I'm running in to a big problem.
In the search operation I'm adding YTVideos (a subclass of NSObject) to a NSMutableArray. When I loop thru it in the main(), I'm getting nil-objects:
Method
- (NSArray *)videosInRange:(NSRange)range {
if(range.length > 50) {
[NSException raise:#"Range lenth > 50"
format:#"The range of -videosInRange: can't be bigger than 50"];
return nil;
}
if((range.location + range.length) > 999) {
[NSException raise:#"Range to big"
format:#"The given range was to big (%d, %d)", range.location, range.length];
return nil;
}
NSString *searchURLString = [[self feedURL] absoluteString];
searchURLString = [searchURLString stringBySettingURLAttribute:#"start-index" value:[NSString stringWithFormat:#"%d",range.location + 1]];
searchURLString = [searchURLString stringBySettingURLAttribute:#"max-results" value:[NSString stringWithFormat:#"%d",range.length]];
NSLog(#"%#",searchURLString);
NSURL *url = [NSURL URLWithString:searchURLString];
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:url
options:0
error:NULL];
if(!xmlDoc)
return nil;
NSArray *videoElements = [[xmlDoc rootElement] elementsForName:#"entry"];
NSMutableArray *videos = [[NSMutableArray alloc] initWithCapacity:[videoElements count]];
register int i;
for(i = 0; i < [videoElements count]; i++) {
NSAutoreleasePool *addPool = [[NSAutoreleasePool alloc] init];
YTVideo *vid = [[YTVideo alloc] initWithXMLElement:[videoElements objectAtIndex:i]];
[videos addObject:vid];
[vid release];
[addPool drain];
}
NSArray *retValue = [NSArray arrayWithArray:videos];
[videos release];
return retValue;
}
main()
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
YTSearchFeed *feed = [[YTSearchFeed alloc] initWithSearch:#"Eminem"];
long long results = [feed videoCount];
NSLog(#"%lld videos for search", results);
long long i = 0;
while(results != 0) {
int length = (results >= 50) ? (50) : (results);
NSArray *videos = [feed videosInRange:NSMakeRange(i, length)];
NSLog(#"L: %d", [videos count]);
int z;
for(z = 0; z < [videos count]; z++, i++) {
YTVideo *vid = [videos objectAtIndex:z];
NSString *title = [vid title];
NSLog(#"%d: %#", i+1, title);
}
results -= length;
}
[pool drain];
return NSApplicationMain(argc, argv);
}
I hope someone can take the time to look at this, and if you need anymore information, just ask.
Thank you in advance,
ief2
EDIT: YTVideo
- (id)initWithXMLElement:(NSXMLElement *)element {
self = [super init];
if(self != nil) {
_XMLElement = [element copy];
}
return self;
}
- (NSString *)title {
if(!_title) {
NSString *str = [[[self XMLElement] firstElementWithName:#"title"] stringValue];
_title = [[str stringByDecodingHTMLEntities] retain];
}
return [[_title copy] autorelease];
}
I get the title (and other video information) only when it's requested. the -stringByDecodingHTMLEntities works fine (Category on NSString).
I've rewritten the code and initialized all instance variables in the -initmethod

What is the value of NSEnumerator?

when for . . . in . . . is available?
Specifically, when we can write:
NSArray *array;
// array allocated and initialized here.
for (id obj in array) {
// do something to the object here
}
Why would we ever use an NSEnumerator?
NSEnumerator was created before fast enumeration (for/in loop) was available. Think of it as backward-compatibility if you like.
But with NSEnumerator you can enumerate the collection in customized order, e.g. backwards:
NSEnumerator* enu = [array reverseObjectEnumerator];
id object;
while ((object = [enu nextObject])) {
...
}
(Of course, since NSEnumerator also supports for/in loop you can use a better way:
for (id object in [array reverseObjectEnumerator]) {
...
}
)
or define your own iterator class by subclassing NSEnumerator, e.g.
#import <Foundation/Foundation.h>
#interface RangeEnumerator : NSEnumerator {
int cur, len;
}
+(RangeEnumerator*)enumeratorWithLength:(int)length;
-(id)initWithLength:(int)length;
-(id)nextObject;
#end
#implementation RangeEnumerator
-(id)initWithLength:(int)length {
if ((self = [super init]))
len = length;
return self;
}
+(RangeEnumerator*)enumeratorWithLength:(int)length {
return [[(RangeEnumerator*)[self alloc] initWithLength:length] autorelease];
}
-(id)nextObject {
if (cur < len)
return [NSNumber numberWithInt:cur++];
else
return nil;
}
#end
int main () {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
for (NSNumber* num in [RangeEnumerator enumeratorWithLength:12])
printf("%d\n", [num intValue]);
[pool drain];
return 0;
}