• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMutableArray & Pointers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableArray & Pointers


  • Subject: Re: NSMutableArray & Pointers
  • From: Jeff Gilbert <email@hidden>
  • Date: Thu, 21 Oct 2004 08:13:29 -0500

Hi Michael,

On Thursday, October 21, 2004, at 07:42AM, Michael Becker <email@hidden> 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?

As soon as you remove the image from the array the reference count on the image goes to zero so the object is released (and the memory deallocated). If you want to inspect the image after it is removed from the array you must first retain the pointer to keep the reference count from reaching zero. When you are finished with the image you will need to release your reference.

For example, you could change the last bit of code to look something like this:

NSImage *pointer = [[array objectAtIndex:0] retain]; // retain the pointer for our own use
NSLog(@"Pointer: %@", pointer);

[array removeObjectAtIndex:0];
NSLog(@"Pointer: %@", pointer);
[pointer release]; // release our reference to the image


Jeff Gilbert

_______________________________________________
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

References: 
 >NSMutableArray & Pointers (From: Michael Becker <email@hidden>)

  • Prev by Date: Re: How does @protected work in obj-c?
  • Next by Date: creating a framework, linker troubles
  • Previous by thread: Re: NSMutableArray & Pointers
  • Next by thread: How does @protected work in obj-c?
  • Index(es):
    • Date
    • Thread