• 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
retain, copy, release, autorelease...leak
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

retain, copy, release, autorelease...leak


  • Subject: retain, copy, release, autorelease...leak
  • From: "Richard Collyer" <email@hidden>
  • Date: Thu, 28 Jun 2001 11:12:20 -0700
  • Organization: Adobe Systems, Inc.

I have what seems like should be simple code, but depending on how I deal with
memory, I either crash with a SIGINT or have a memory leak.

If I release the NSColor when I think I should, the code will crash. If I don't
release the NSColor object, then there is a memory leak.

Can anyone give me a clue what I am am doing wrong. What did I miss about
memory allocation and retention in Cocoa?

++++++++++++++++++++
#import "testArray.h"

@implementation testArray

- (id) init {
NSMutableArray *myArray;
int x, y;
NSColor *colorStore;

// IS THIS RETAIN REDUNDANT (I.E. BAD)?
myArray = [[NSMutableArray arrayWithCapacity: 10] retain];

for (x = 0; x< 10000; ++x) {
colorStore = [self Quicky];
// it is my understanding that addObject
// will retain the object added to the array
// so it will make the retain count 2
// (since autorelease has not happend yet)
[myArray addObject: colorStore];
// If I release (or autorelease) colorStore, I crash,
// but if I don't I have a leak
//[colorStore release];
}

for (y = 0; y < 100; ++y) {
for (x = 0; x< 10; ++x) {
colorStore = [self Quicky];
// it is my understanding that replaceObjectAtIndex
// will release the old and retain the new.
[myArray replaceObjectAtIndex: x withObject: colorStore];
//[colorStore release];
}
}
// Bottom line question is what am I missing?
[myArray release];
self = [super init];
return self;
}


- (NSColor *) Quicky {
NSColor *finalColor;
// as best I can tell colorWithDeviceRed
// does an NSColor alloc, init, and autorelease
// so I should not need to do anything with f
// inalColor, it will just go away in the next
// event loop within the Cocoa foundation

finalColor = [NSColor colorWithDeviceRed:1 green:1 blue:1 alpha:1];

// I have found that if I do try to
// autorelease finalColor, then I crash

return finalColor;
}
@end

++++++++++++++++++++

--
Rich Collyer


  • Follow-Ups:
    • Re: retain, copy, release, autorelease...leak
      • From: Fritz Anderson <email@hidden>
  • Prev by Date: Re: addTrackingRect fails?
  • Next by Date: Re: AppKit source available as reference?
  • Previous by thread: Re: Sit on it and rotate ??
  • Next by thread: Re: retain, copy, release, autorelease...leak
  • Index(es):
    • Date
    • Thread