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

  • Home
  • SEARCH
  • 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 3805624
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:42:27+00:00 2026-05-19T14:42:27+00:00

Suppose I have two classes. In the first one I declare this in Class1.h

  • 0

Suppose I have two classes. In the first one I declare this in Class1.h

@interface Class1 : UIViewController {

  NSString *myString;
  id myObject;

}

On the second class I go beyond that I declare it like

@interface Class2 : UIViewController {

  NSString *myString;
  id myObject;

}

@property (nonatomic, retain) NSString *myString;
@property (nonatomic, retain) id myObject;

and then I @synthesize myString, myObject on Class2.m

Then, on my main program, I create two objects: one based on Class1 and another one based on Class2.

What effect the @property of class2 will have? Will every value assigned to both values on Class2 be always retained? If so, do I need to “release” them? How?

Thanks.

  • 1 1 Answer
  • 3 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-19T14:42:27+00:00Added an answer on May 19, 2026 at 2:42 pm

    Please read Declared Properties section of The Objective-C programming language
    for a full explanation on properties 😉

    In Class2:

    In this case you set retain attribute to your property it is supposed to be retained in the implementation. This is done automatically when you synthesize a property.
    This means that you should have

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

    and everything should be fine

    In Class1, you don’t have properties so myString and myObject is not visible from outside. But this does not mean that you shouldn’t release them. It depends on the way you initialize them and/or if you send retain messages to them.

    BTW, if you set assign a property you don’t release it, just set it to nil in the dealloc method. If you set copy to it then you must release it.

    EDIT

    You said: *But suppose I have this *

    @property (nonatomic, retain) UIView *myView; 
    

    and

    myView = [[UIView alloc] initWithFrame:myFrame]; 
    [self.view addSubview:myView]; 
    [myView release];
    

    ? I am already releasing myView… do I have to release it again???

    First, since you have your property defined that way, you should have dealloc method as:

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

    So, the answer is NO you should not release it but actually is not correct.
    Please take a look:

    myView = [[UIView alloc] initWithFrame:myFrame];  //myView retainCount is 1
    [self.view addSubview:myView];  //retainCount is 2
    [myView release]; //retainCount is 1 again
    

    later in dealloc method

    - (void) dealloc{
        [myView release]; // retainCount becomes 0, is deallocated
        [super dealloc]; // subviews of self.view are released but myView was already deallocated!, so you have over released myView once ;(
    }
    

    This is the correct way: (Use your properties 😉 )

    UIView *aView = [[UIView alloc] initWithFrame:myFrame]; // init, retainCount is 1
    self.myView = aView; // retainCount becomes 2
    [aView release]; // retainCount becomes 1 again and we are fine.
    
    [self.view addSubview:self.myView]; //retainCounts becomes 2 again.
    

    even if it is 2 there is no problem because when self.view is deallocated its subviews also will be released. Hence self.myView retainCount will become 1 again later when self is deallocated.

    - (void) dealloc{
        [myView release]; //retainCounts becomes 1
        [super dealloc];  // all its subviews of self.view are released hence myView retaincount becomes 1 and is released corretly
    }
    

    What is the difference?

    Suppose self.myView is also retained by other object X and with the former approach, X’s view will be pointing to an invalid object, because it was already released.

    Hope it helps

    EDIT2
    As bbum’s indication, this is a mini-mini-short tutorial on properties:

    when you have

    @property (... retain) NSObject *retainVar;
    @property (... assign) NSObject *assignVar;
    @property (... copy) NSObject *copyVar;
    

    and you @synthesize them

    is like having the following setters:

    // retain
    -(void)setRetainVar:(NSObject *)var {
         if (retainVar != var) {
            [retainVar release];
            retainVar = [var retain];
        }
    }
    //assign
    -(void)setAssignVar:(NSObject *)var {
         assignVar = var;
    }
    //copy
    -(void)setCopyVar:(NSObject *)var {
        if (copyVar != var) {
            [copyVar release];
            copyVar = [var copy];
        }
    }
    

    (this means that if you assign directly an object you have to make sure is something equivalent to above setters, from the memory management point of view)

    and your dealloc method should be something like:

    - (void) dealloc{
        [retainVar release];
        assignVar = nil;
        [copyVar release];
        [super dealloc];
    }
    

    When setting your ivars

    for example, inside of init:

    - (id) init{
        if ((self = [super init])){
    
            //this is ok
            retainVar = [[NSObject alloc] init];//but is retainVar was not nil we will have a leak ;(
    
            //This is better
            NSObject *obj = [NSObject alloc] init];
            self.retainVar = obj;
            [obj release]; 
    
            //this is BAD
            assignVar = [[NSObject alloc] init];//because this is like retaining it, later it will leak
    
            //below is correct
            NSObject *obj = [[[NSObject alloc] init] autorelease];
            assignVar = obj;
    
            //copy is pretty much like retain,
            //this is ok
            copyVar = [[NSObject alloc] init]; //but, if copyVar was not nil is a leak!
    
            //below is better
            NSObject *obj = [NSObject alloc] init]:
            self.retainVar = obj;
            [obj release];
    
         }
         return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose that I have two classes, first a class without any properties, fields or
Suppose I have two classes: class1 { int m_i; std::string m_s; }; class2 {
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose we have two classes: class Base { private: int x; public: void f();
suppose you have two (or more) classes with private member vectors: class A {
Supposed I have two classes like this: public class Parent { public string Id
Suppose you have a Django view that has two functions: The first function renders
Suppose I have two classes defined the following way: public class A { public
I have a question relating to the usage of this. Suppose I have two

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.