Re: NSMutableArray & Pointers
Re: NSMutableArray & Pointers
- Subject: Re: NSMutableArray & Pointers
- From: stephane sudre <email@hidden>
- Date: Thu, 21 Oct 2004 14:58:36 +0200
On Oct 21, 2004, at 2:44 PM, Michael Becker wrote:
Hi!
I'm running into the following problem and don't know how to get
around it: I have an NSMutableArray that holds NSImages. Then I
specify a pointer of type NSImage* which points to an object in the
array. When that object is removed from the array, I would like to be
able to check whether the pointer still points to a valid NSImage.
Here's a piece of code:
NSMutableArray *array = [[ NSMutableArray alloc] init];
NSImage *image = [[ NSImage alloc]
initWithContentsOfFile:@"/Users/michael/Pictures/anyPicture.jpg"];
[ array addObject:image];
[ image release];
NSImage *pointer = [ array objectAtIndex:0];
NSLog(@"Pointer: %@", pointer);
[ array removeObjectAtIndex:0];
NSLog(@"Pointer: %@", pointer);
The output is:
2004-10-21 14:42:23.575 PointerTest[2345] Pointer: NSImage 0x331f80
Size={108, 144} Reps=(
NSBitmapImageRep 0x33d880 Size={108, 144}
ColorSpace=NSCalibratedRGBColorSpace BPS=8 Pixels=450x600 Alpha=NO
)
2004-10-21 14:42:23.575 PointerTest[2345] Pointer: Pointer:
What is happening to the pointer-Variable?? As soon as I try to invoke
[ pointer size] in the last line of my code, the app crashes. Is there
any way to be able to reasonably perform an "if (pointer==nil)"
statement?
AFAIK, it mostly depends on whether [ objectAtIndex:] is returning the
object directly or a [[retain] autorelease] pointer. Probably the first
one. Anyway, it's up to you to manage retaining/releasing. If you
really want to keep track of pointer, you need to retain it before
calling removeObjectAtIndex:.
Finally, why pointer should be(come) nil when it's deallocated?
_______________________________________________
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