|
|
will this cause a memory leak?
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
Here is what I am trying to do...
I have a class Account. And the name is returned with accountName
I have a searchfield and as i type i filter my table of accounts on the fly... i want it to be case insensitive search.
Everything works great.
I'm wondering though if by creating the lowercase strings for this comparison I am causing a memory leak. I never release the pointer that is returned to me for the lowercase string. Is this taken care of? If so how (autoRelease maybe?)?
NSRange subStringRange = [[[account accountName] lowercaseString] rangeOfString:[[searchField stringValue] lowercaseString]];
if(subStringRange.length)
{
// if you are here, the search field contains is a sub string of the account name.
}
|
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Stuff that isn't alloced or copied is returned autoreleased, so no, you won't be leaking with that code.
(You can check by releasing them, and seeing if you crash ).
Also, you could do:
NSRange subStringRange = [[account accountName] rangeOfString:[searchField stringValue] options:NSCaseInsensitiveSearch];
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
what if i were to save the returned string pointer and use the lowercased-string in a few places... am i still ok?
i'll try the options param as well... didn't see that when poking thru the headers
|
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Then you should retain it, because you'll become an owner of the string.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
so NSString *myLowercaseString = [aString lowercase]; retains?
but this is ok
[aString lowercase];
|
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
No, [[aString lowercaseString] retain] retains. I suggest you read up on Cocoa memory management. (Not trying to be rude; you just seem a little fuzzy on the topic, which won't do you any good.)
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
ok... reread my little beige book and made a test app.. i got it. Thanks!
|
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jan 2003
Status:
Offline
|
|
Autoreleasing confused me for a while, but it's actually pretty simple. Most functions in Cocoa return objects that are put into the autorelease pool (the only exception i know of is the [[class alloc] init] call). This means that they will be destroyed when control of the application returns to Cocoa, i.e., once your function returns. So, if you want a class for longer than that you have to take control of it by [class retain] ing it. Once you retain it, you also have to release it. But in general, if you don't ever retain or release a class it stays around until you return your function (or if you are doing autoreleasepools).
Caio
Daniel
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Technically, they aren't released when your function returns; they're released at the end of the current run loop iteration. But your advice still holds true.
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Think of it as ownership. If you explicitly create an object (e.g. with alloc, new, copy or mutableCopy), you own that object � you control when it goes away. But if you ask for an object from another object, you need to retain that object in order to keep it. The only thing that's at all vague about the system is that with factory methods like [NSArray array], it's NSArray that's doing creating the object, so you have to retain it if you want to own it.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
thanks everyone.. i've been NSLogging the retain count and I think I've got it figured out!
|
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|