Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saqibjaan

macrumors member
Original poster
Feb 1, 2012
48
1
Lahore
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
 
Last edited by a moderator:
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 :)
 
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.
 
Try adding a log statement to your Rectangle's dealloc method to verify that it is being deallocated.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.