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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:52:24+00:00 2026-05-23T08:52:24+00:00

I am new to objective-c and i cannot figure out how memory handling works

  • 0

I am new to objective-c and i cannot figure out how memory handling works exactly in this language. Here is some code i wrote from a turorial and i am confused why when i uncomment the [filePath release] i get an error even though the method is finished. I read some articles on how memory handling works but i cant see what i am doing wrong here.

#import "saaving_dddaaattaViewController.h"

@implementation saaving_dddaaattaViewController
@synthesize field;

-(NSString *)pathOfFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [paths objectAtIndex:0];// paths[0] = documents directory
return [documentsFolder stringByAppendingFormat:@"myfile.plist"];
}

-(void)applicationWillTerminate:(NSNotification *)notification {
NSLog(@"Saving data...");
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:field.text];
[array writeToFile:[self pathOfFile] atomically:YES];
[array release];
}

- (void)dealloc {
[field release];
[super dealloc];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
NSString *filePath = [self pathOfFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
    NSLog(@"File[%@] does exist.", filePath);
    NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
    field.text = [array objectAtIndex:0];
    [array release];
}

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];

//[filePath release];// <--- commented out release
[super viewDidLoad];
}

- (void)viewDidUnload {
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@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-23T08:52:25+00:00Added an answer on May 23, 2026 at 8:52 am

    Memory Management can be confusing at first. Only release objects that you have created. This is almost only if you use the words alloc, init, retain.

    The problem in your case is that you do not actually own the object, it is autoreleased, because you did not alloc, init or retain it. This is fine, but it is not your job to release it, so don’t worry about it.

    If you get “EXC_BAD_ACCESS” errors later, it might be helpful to use NSZombies to help find where you are releasing incorrectly. They work by placing a “zombie” in memory wherever you release an object so it is easier to tell what the problem is.

    EDIT: For example, say you have:

    NSString *foo = [[NSString alloc] initWithString:@"foo"];
    
    NSString *bar = [NSString stringWithString:@"bar"];
    

    You would have to release foo, by calling: [foo release]; at some point, but you would not have to release bar because it you did not use alloc to allocate memory for it. This goes for any type of object, not just NSString. A great website explaining this can be found here.

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

Sidebar

Related Questions

I am new to iPhone apps and objective c and I cannot figure out
I am new to Objective-C and I have no clue why this code is
I'm new to Objective-C here, and still learning its syntax, so I'm experimenting some
Still new to Objective C, and I'm having some trouble that I just can't
I'm new with Objective-C, so there probably is a simple solution to this. I
First, I am super-new to Objective-C/iOS development and, in fact, this question is for
I am still new in objective-c programming, I got this exception, but I don't
Alright, I've spent about 5 hours trying to figure this out. Absolutely none of
I am fairly new with Objective-C memory management and although I thought I understood
I'm new in Objective - C... I found source code how to use OpenAl,

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.