need help understanding and learning memory managment..
need help understanding and learning memory managment..
- Subject: need help understanding and learning memory managment..
- From: "Brian O'Brien" <email@hidden>
- Date: Wed, 31 Aug 2005 11:35:10 -0600
I have read a few docs on memory management in Cocoa.
But there are a few concepts that the docs don't seem to be helping
me with.
For one thing is it possible to print out the current retain counter
of an object?
Just for some debugging purpose?
I have three classes. ClassA calls classB to get back an array of
objects of classC.
The ownership of the array of classC objects belongs to classA and
should be released when classA goes out of
scope.
I'm trying to make sure that there is no memory leak here.. What I
expect to see is
1 call to classBs dealloc
2 call to classAs dealloc
3 10 calls to classCs dealloc
This code is just a proof of concept but I would appreciate some help
tying to get it to work...
with the retains/releases etc.. I will eventually put a memory pool
around this if I am unable to due it without one.
Can I test this from the command line like a c/c++ console
application or do I need a nib et al to do this?
@implementation classA
-(void) dealloc
{
NSLog(@"Deallocating classA");
[super dealloc];
}
-(void) method
{
classB * b = [[classB alloc] init];
NSMutableArray * ac = [b getNCs(10)];
}
@end
@implementation classB
-(void) dealloc
{
NSLog(@"Deallocating classB");
[super dealloc];
}
- (NSMutableArray *) getNCs:(int)n
{
NSMutableArray *ret = [NSMutableArray arrayWithCapacity:n];
for (int i=0; i < n; ++i)
{
classC * c = [[classC alloc] init];
[ret addObject:c];
}
return ret;
}
@end
@implementation classC
-(void) dealloc
{
NSLog(@"Deallocating classC");
[super dealloc];
}
@end
main()
{
classA *a = [[classA alloc] init];
[a method];
[a release];
// do it again.
classA *a = [[classA alloc] init];
[a method];
[a release];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden