{"id":3999,"date":"2014-03-30T07:04:07","date_gmt":"2014-03-30T07:04:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/memory-management-headache-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:04:07","modified_gmt":"2014-03-30T07:04:07","slug":"memory-management-headache-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/memory-management-headache-collection-of-common-programming-errors\/","title":{"rendered":"Memory Management Headache-Collection of common programming errors"},"content":{"rendered":"<p>I get leaks if I dont put it in dealloc. I get a crash <code>EXC_BAD_ACCESS<\/code> If I do. I cannot see anything wrong with this code. The bad access is pointed at <code>[events release]<\/code>. Have I made a mistake in the code below or is Instruments just having a laugh at my expense?<\/p>\n<p><code>events<\/code> is an <code>NSArray<\/code><\/p>\n<pre><code>@interface EventsViewController : UITableViewController \n{    \n@private\n    NSArray *events;\n}\n\n - (void)viewDidLoad\n    {\n        events = [[self getEvents] retain];\n    }\n\n    - (void)dealloc\n    {\n        [events release];\n        [super dealloc];\n    }\n\n    - (NSArray*)getEvents\n    {\n        NSMutableArray *response = [[[NSMutableArray alloc] init] autorelease];\n\n       \/\/Some sql\n       while(sqlite3_step(statement) == SQLITE_ROW)\n       {\n           Event *event = [[[Event alloc] init] autorelease];\n           event.subject = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];\n\n          [response addObject:event];\n       }\n       return response;\n    }\n<\/code><\/pre>\n<p><strong>Update<\/strong> A lot of you are saying the code is fine which is a plus. I dont manipulate <code>events<\/code> elsewhere &#8211; I have removed any code that does to try and single out the crash. Perhaps its in the parent view?<\/p>\n<p>This is the click event that pushes the EventsViewController: &#8211; (void)eventsClick:(id)sender<\/p>\n<pre><code>{\n    EventsViewController *eventsViewController = [[EventsViewController alloc] initWithNibName:@\"EventsViewController\" bundle:nil];\n    eventsViewController.anywhereConnection = anywhereConnection;\n    eventsViewController.contact = contact;\n\n    [[self navigationController] pushViewController:eventsViewController animated:YES];\n    [eventsViewController release];\n}\n<\/code><\/pre>\n<p>The crash is actually happening when I return to the parent view. (I think it is considered a parent in this scenario). But perhaps the <code>[eventsViewController release]<\/code> just triggers <code>dealloc<\/code> in the <code>EventViewController<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I get leaks if I dont put it in dealloc. I get a crash EXC_BAD_ACCESS If I do. I cannot see anything wrong with this code. The bad access is pointed at [events release]. Have I made a mistake in the code below or is Instruments just having a laugh at my expense? events is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3999","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3999"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3999\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}