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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:30:46+00:00 2026-05-13T15:30:46+00:00

first question here, this is regarding the iPhoneOS3 not MacOSX. I am fairly new

  • 0

first question here, this is regarding the iPhoneOS3 not MacOSX. I am fairly new to Objective-C and I’ve never developed in a environment without automatic garbage collection so I am a little confused by this. Here’s some valid code assigning a view controller to an app delegate from an example off Apple.com:

MyViewController *aViewController = [[MyViewController alloc]
    initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];

So, from what I understand, I must release aViewController because it is first allocated (+1 = 1); then retained in the setter (+1 = 2); then released in the setter (-1 = 1); and then no longer needed so finally released again (-1 = 0) and the memory is freed. Wel could I not just skip assigning the temporary object aViewController and nest these functions like so:

[self setMyViewController:[[MyViewController alloc]
    initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]]];

I was wondering if this would work correctly? I am a little worried since the setter requires a pointer-to-ViewController instead of just a copy of one. And since I am only passing a return value, is the pointer-to-ViewController in the setter going to point to data that may be erased or lost before it can assign it? I am sorry if this seems like a dumb question, but I am having a hard time finding an answer anywhere, and I am trying to establish good techniques for non-garbage collection environments. Thanks!

  • 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-13T15:30:46+00:00Added an answer on May 13, 2026 at 3:30 pm

    Don’t think of memory management in terms of absolute retain counts. Think of it entirely in terms of ownership and encapsulation.

    When you alloc an object, you have created an object that you own. When you set that object as a value on another object, that other object should retain the object to express ownership. If your code is no longer interested in the object, you should then release (or autorelease it if you want to return it to something else).

    [self setMyViewController:[[MyViewController alloc]
        initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]]];
    

    To answer your specific question, the above line of code, by itself, will cause a memory leak assuming that -setMyViewController: is implemented correctly.

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

Sidebar

Related Questions

this is my first question here :) I know that I should not check
its my first question here on this site. well i have a problem regarding
this is my first question here so I hope I can articulate it well
Greetings to all! This is my first question here on stackoverflow. I have a
Ok, I need help. This is my first question here. Background: I am working
this is my first question to stackoverflow so here it goes... I use cruise
this is my first question.. so, here we go. i have a site, 100%
Sorry, this's my first time to ask a question here. So, I don't have
first of all this is my third question about web services here and i
This is my first time here so I hope I post this question at

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.