Re: Memory Managment (copys??)
Re: Memory Managment (copys??)
- Subject: Re: Memory Managment (copys??)
- From: Sherm Pendley <email@hidden>
- Date: Sat, 5 Feb 2005 17:55:18 -0500
On Feb 5, 2005, at 4:38 PM, Coleman Nitroy wrote:
Ok, I am very suprised I got as far as I did without running into a
memory issue. Here is my perplexing problem (its probably something so
very simple).
If I were to do this
NSMutableArray *array = [[NSMutableArray alloc] init];
SOMEObject *object = [[SOMEObject alloc] init];
for ( i = 0; i < 10; i++ ) {
[object setSomeProperty: i];
[array addObject: object];
}
And then print out someProperty for all of the objects in array, I
would get the last property only.. so I would get 10, 9's.
The array has ten references to the same object.
I tried doing:
[array addObeject: [object copy]];
And all I got was 10, 0's. I am baffled. How do I handle this?
I'm guessing that you haven't implement your own -copyWithZone: method
in your SOMEObject class. From the docs for the NSCopying protocol:
"If a subclass inherits NSCopying from its superclass and declares
additional instance variables, the subclass has to override
copyWithZone: to properly handle its own instance variables, invoking
the superclass’s implementation first."
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
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