NSNumber numberWithInt: is causing memory leak-Xcode - objective-c

I've been working on the leaks in my program today and I understand most of them but this one is throwing me for a loop:
[startAndEndPoints addObject:[NSNumber numberWithInt:buttonTag]];
The Leaks instrument is pointing to this saying that it is 100% of a leak. Can anyone advise?
startAndEndPoints is a NSMutableArray.

Did you compare your situation to iphone app NSNumber memory leak ? Leaks is telling you how the leaked object got created, not where the leak is. You're probably handing this NSNumber object off to someone else later, and that someone else is over-retaining it.

Related

Objective-C Memory Leaks

I'm just investigating some memory leaks in my app, I'm using Xcode 4.0.2. I've run the Analyze tool in Xcode and a good few memory leaks have been identified. I'm relatively new to Objective-C, this is my first app. I've pasted the code here:
http://pastie.org/3155043
I've added comments to the above code, where the memory leaks are occuring.
Memory Leak One: Method returns an Objective-C object with a +1 retain count (owning reference).
Memory Leak Two: Object allocated on line 248 and stored in 'imagesNames' is not referenced later in this execution path and has a retain count of +1 (object leaked).
Memory Leak Three: Potential leak of an object allocated on line 246 and stored into 'cmpWordoutStr'.
Any help appreciated.
Regards,
Stephen
You might want to consider using Automatic Reference Counting in your project.
I asked a question the other day on here, as I wasn't sure that it was a good idea, but the answers convinced me that it really is a step forward and is well worth taking advantage of:
To ARC or not to ARC? What are the pros and cons?
Hope this helps :)
Leak 1) You don't show the return or identify which variable is returned, so not possible to definitively diagnose this one.
Leak 2) You alloc/init an NSString and assign it to a variable that is never released. This is wrong for two reasons:
For each alloc there must be a corresponding release somewhere.
There is no point in doing alloc/init on an empty string. If you want an empty string just use #"".
Leak 3) Basically the same as (2).
(You really need to get a good book on Objective-C programming and study and restudy the section on storage management. Otherwise you'll be stumbling around in the dark.)
You're allocating an object first
NSString *cmpWorkoutStr = [[NSString alloc] init];
and then reassign the pointer without freeing the memory:
cmpWorkoutStr = [cmpWorkoutStr stringByAppendingString:indWorkoutStr];
hence the leak.
I didn't analyze your code in depth, but I guess you actually want NSMutableString there.
As Tom Andersen suggested above I used auto release and this solved the problem, example below:
NSString *cmpWorkoutStr = [[[NSString alloc] init] autorelease];
NSString *imageNames = [[[NSString alloc] init] autorelease];
Regards,
Stephen

Objective-C: Why is this a memory leak?

I am getting a memory leak in my objective-C code that I don't understand. I have this code in a method that gets called several times:
AnalyzeBpm *analyzer = [[AnalyzeBpm alloc] init];
while( sample != NULL)
{
//do something with analyzer
}
[analyzer release];
When I run this code through Instruments, I get a leak everytime I allocate Analyze Bpm(which is a couple of hundred times). I looked at my AnalyzeBpm class, and everything I allocate in that class gets freed or deallocated. So why is this code creating a memory leak?
When Instruments identifies a leak, it is showing you the line of code that is allocating the leak, not the line of code that causes the leak.
Somewhere something is retaining analyzer without releasing it. You need to find that unbalanced retain. It may or may not be in the AnalyzeBpm class.
Your alloc-init and release are balanced, so it has to be something else — something in your while loop.

iOS memory leak with dictionary

I'm currently profiling my app and coming across a few leaks. I've tried releasing objects all over the place where I think they are needed. Each release has crashed the app.
Here's one line that I think is the culprit:
NSDictionary *dicUserData = [NSDictionary dictionaryWithObject:self forKey:#"chapter"];
Just wondering the best way to deal with this. I'm having issues with other leaks too that are kind of similar. It's worth noting that self is a custom class [Dal_Chapter].
Do I need to implement copy or something, call that in the above line and do autorelease on that?
Thanks in advance.
Using convenience methods such as dictionaryWithObject provide an autoreleased instance of the dictionary object. Unless you're retaining it elsewhere this is not where your leak is.
I would check out the static analyser it should be able to point out your leaks for you.
That dictionary will retain self so you shouldn't have to worry about it.
Is the owner of self releasing it ?
Basicly you don't have to do anything whith your dictionnary, but if you want to take control of the memory you'll need to do this :
NSDictionary *dicUserData = [[NSDictionary alloc] initWithObjects:yourObject forKeys:key];
and int your dealloc method,
[dicUserDate release];
But normaly you just have nothing to do... Are you sure that your leak come from your dictionnary ?
always keep all variables release in dealloc method. is it not in proper place it may be crash .check this link click here

Memory leak in Instruments

running my app through Instruments "Leaks" it's saying I've got a leak which seems to happen with this code -
-(void)podAppears {
podCount ++;
NSString *podName = [NSString stringWithFormat:#"Pod%i",podCount];
Pod *thePod = [[Pod alloc] initWithOwner:self withName:podName];
[pods setObject:thePod forKey:podName];
[thePod release];
}
I can't see anything wrong, but I'm fairly new to Objective-C & memory management in general. Any help much appreciated!
That code looks fine. When Instruments informs you of a leak, it tells you where the leaked object originated. It's likely that the actual leaking of that object is occurring elsewhere in your app. You should look at other locations where you access your Pod objects.

Memory leak in stringByAppendingString

NSString *strURL=#"http://cache.finn.no/mmo/";
strURL=[strURL stringByAppendingString:soapResults];
[arrMainImages addObject:strURL];
My last two lines are causing memory leaks? Here, the variable soapResults is of type NSMutableString.
The snippet above looks fine regarding how you are using strURL - how do you actually know that there are leaks occuring in those lines?
You might want check though that you are releasing soapResults and arrMainImages properly if you took ownership of them.