Is there any memory leak in this code?

Discussion in 'iOS Programming' started by saqibjaan, Jun 21, 2012.

  1. saqibjaan, Jun 21, 2012
    Last edited by a moderator: Jun 21, 2012

    saqibjaan macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #1
    A simple program which implements composition in Objective C.
    Rectangle class object is an instance variable of Square class. My question is that is there any memory leak in my code?

    Code:
    int main (int argc, const char * argv[])
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // 'squ' is a composite object because it is composed of other objects.
    	Square *squ = [[Square alloc] initWithSide:5.0];
    	
    	NSLog(@"Side of Square is %.2f", [squ side]);
    	
    	NSLog(@"Area of Square is %.2f", [squ area]);
    	
    	NSLog(@"Perimeter of Square is %.2f", [squ perimeter]);
    	
    	[squ release];
    	
        [pool drain];
        
    	return 0;
    }
    /////////////////////////////////////////////////////////////////
    
    @interface Square : NSObject
    {
    	Rectangle *rect;
    	
    }
    
    
    -(Square *)initWithSide:(float) s;
    -(void)setSide:(float) s;
    -(float)side;
    
    -(float)area;
    -(float)perimeter;
    -(id)dealloc;  // Override to release the Rectangle object's memory
    
    @end
    ////////////////////////////////////////////////////////
    
    @implementation Square
    
    -(Square *) initWithSide:(float) s
    {
    	self = [super init];
    	
    	if(self)
    	{
    		rect = [Rectangle new];
    		rect.width = s;
    	}
    	
    	return self;
    }
    
    
    -(void)setSide:(float) s
    {
    	rect.width = s;
    }
    
    
    -(float)side
    {
    	return rect.width;
    }
    
    
    -(float) area
    {
    	return ((rect.width) * (rect.width));
    }
    
    
    -(float) perimeter
    {
    	return (4 * (rect.width));
    }
    
    
    -(id) dealloc
    {
    	[rect release];
    	
    	[super dealloc];
    	
    	return self;
    }
    
    @end
    ///////////////////////////////////////////////////////////////
    
    @interface Rectangle : NSObject
    {
    	float width;
    	float height;
    }
    
    @property float width, height;
    
    -(float) area;
    -(float) perimeter;
    
    -(void)setWidth:(float) w andHeight:(float) h;
    
    @end
    //////////////////////////////////////////////////////////////
    
    @implementation Rectangle
    
    @synthesize width, height;
    
    
    -(void)setWidth:(float) w andHeight:(float) h
    {
    	width = w;
    	height = h;
    }
    
    
    -(float)area
    {
    	return (width * height);
    }
    
    
    -(float)perimeter
    {
    	return ((width + height) * 2);
    }
    
    @end
    
     
  2. Scott90 macrumors 6502

    Joined:
    Jul 14, 2008
    #2
    I don't think so, but your code isn't really well readable without code tags.
     
  3. Tander macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #3
    Also, you can check for memory leaks in Xcode 4 and see where it is.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    The above poster means, "Press Command Shift B, this will start the analyzer which will tell you if there are any memleaks" (they don't show all, but it's a hint).
    Second, like the guy above that guy said, please use code tags to make your code readable in a post :)
     
  5. saqibjaan thread starter macrumors member

    Joined:
    Feb 1, 2012
    Location:
    Lahore
    #6
    Is there any memory leak in this code?

    Rectangle object (rect) is an instance variable of Square class.
    I have initialized rect object in the initWithSide method of Square class.
    And I have just release the rect object in dealloc method.

    Is this sufficient to just sent a release message to rect object?
    Or I have to add some other code to make my program leak free.
     
  6. klaxamazoo macrumors 6502

    Joined:
    Sep 8, 2006
    #7
    Try adding a log statement to your Rectangle's dealloc method to verify that it is being deallocated.
     

Share This Page