Amazon.com Widgets Extension Through Inheritance -- Retangle Class.
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 02:41:10 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Program Examples
| | |-+ Chapter 8
| | | |-+ Extension Through Inheritance -- Retangle Class.
Pages: [1] Go Down
Print
Author Topic: Extension Through Inheritance -- Retangle Class. (Read 1360 times)
Mike
Jr. Member
**
Posts: 51


Email




on: November 15, 2009, 09:00:38 PM

Hi,

I'm not sure how to use the dealloc method to release the origin object.

Thanks
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: November 15, 2009, 09:18:01 PM

Mike.

I'm not sure I understand your question, as this is covered in the text.  Let me know if there's something else you need explained.

Cheers,

Steve Kochan
Logged
Mike
Jr. Member
**
Posts: 51


Email




Reply #2 on: November 16, 2009, 11:02:23 AM

Hi,

Page 180 talks about overriding dealloc.  Here is the code

-(void) dealloc
{
     if (origin)
          [origin release];

     [super dealloc];
}

If I understand correctly, in the implementation section of Rectangle we create a new origin object each time we make a call to setOrigin.  so do we make a call to dealloc within setOrigin before we create a new origin object like this.

-(void) setOrigin: (XYPoint *) pt
{
    [origin dealloc];
    origin = [[XYPoint alloc] init];
    [origin setX:pt.x andY: pt.y];
}
Logged
esc
Global Moderator
Full Member
*****
Posts: 230






Reply #3 on: November 16, 2009, 11:45:49 AM

Hi Mike,

We don't call dealloc directly.  Instead, when we are finished with an object, we send it the release message, e.g.
-(void) setOrigin: (XYPoint *) pt
{
    [origin release];
    origin = [[XYPoint alloc] init];
    [origin setX:pt.x andY: pt.y];
}
Logged
Mike
Jr. Member
**
Posts: 51


Email




Reply #4 on: November 16, 2009, 01:06:24 PM

Hi ESC,

Thank you for your help Cheesy.  Have you wrote anymore apps for the iPhone?  Anyway, where in my program do I place the override for dealloc (shown below)?  Does it go in the Rectangles implementation file?

-(void) dealloc
{
     if (origin)
          [origin release];

     [super dealloc];
}


Thanks
Logged
toby
Full Member
***
Posts: 104






Reply #5 on: November 16, 2009, 01:37:08 PM

Yes you put it in the rectangles implementation section Smiley

To add to what esc said, the reason you don't use dealloc directly is because there may be more than one reference to the same instance within the program. When done correctly a reference count is kept so that the instance is not destroyed until there are no references being made, if you dealloc the instance is destroyed, if you release it then you are reducing the reference count. When the reference count reaches 0 the dealloc method is then called automatically, this actually removes the instance from memory and it is at this point that you want to release other instances that the instance being dealloc'd is relying upon. The memory management chapter goes into this stuff in detail.
Logged
Mike
Jr. Member
**
Posts: 51


Email




Reply #6 on: November 16, 2009, 02:13:29 PM

Hi Toby,

Thanks  Cheesy.  So if there is more than one reference to the object I dealloc(ed) then those references would point to nil?  So good memory management is key.

Thanks
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.