Memory leaks in Iphone - iphone

label = (UILabel *)[cell.contentView viewWithTag:1];
label.text =labelString;
size = [label.text sizeWithFont:[UIFont fontWithName:#"ArialMT" size:14] constrainedToSize:CGSizeMake(320,9999) lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(5, 5, 295, (size.height+25));
UIFont leave 256 Bytes leak.
And some other leaks also present in my app related to web kit and Foundation library.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Prayers" ofType:#"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
These lines leave UIKit WebKit and DataDetectorsCore related leaks.
Please suggest any solution for this problem. Total leaked memory is arround 3 KB, can i
leave this in app or not.

I doubt you have leak on the UITableViewCell instances.
You made some custom UITableViewCells, maybe you need to check those UITableViewCells.

The UIFont leak you mentioned may not be leak at all. The possibility that UIFont wasn't released is internal caching by UIFont when you use the fontWithName method.

I noticed that UIFont may leak memory when called not from the main thread. Put assertion before your code. This will allow you to catch the error.
NSAssert([NSThread isMainThread]);

Related

UITextView Scrolling Memory Issue on iOS7

I have a trouble with UITextView scrolling on iOS7 which didn't happen on iOS6.
When I set text on UITextView and scroll it fast on device, Xcode5 memory report shows it consuming huge memory. However, when I do the same on simulater, it's just fine.
Does anybody has the same issue? I use iOS7 and Xcode5.0, and clean installed iPhone5 for device.
This is the code for the test.
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
NSString *textPath = [[NSBundle mainBundle] pathForResource:#"Alice's Adventures in Wonderland by Lewis Carroll" ofType:#"txt"];
NSString *string = [[NSString alloc] initWithContentsOfFile:textPath encoding:NSUTF8StringEncoding error:nil];
textView.text = string;
textView.font = [UIFont systemFontOfSize:20];
[self.view addSubview:textView];
Sample Project
https://github.com/umekun123/iOS7-UITextView-Scrolling--Test
Profiling Picture

iphone timer plist file display

I'm going to ask this question again,I thought I had figured it out but I guess not. This is for a timer app that gets the timer info from a plist file. Everything works but I want the .plist extension not to show in my tableview. Here are two code snippets that I believe are the issue. These are in an array called NSArray *files;
self.files = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"Timers1"];
// Configure the cell.
cell.textLabel.text = [[files objectAtIndex:indexPath.row] lastPathComponent];
return cell;
Thanks for the help
NSString *text = [[files objectAtIndex:indexPath.row] lastPathComponent];
text = [text substringToIndex:[text rangeOfString:#"."].location];
cell.textLabel.text = text;

UIImageView animation causing crash

I have a UIAnimation view that plays an array of PNG images as an animation. There are about 200 frames and total size is about 8 MB. The animation works just fine on simulator and iPhone 4, but when I test on iPhone 3GS, the app crashes due to the animation.
I've tried using UIImage imageNamed:, but I read that using imageWithData might be faster, so I have this:
NSString *imageName=[NSString stringWithFormat:#"fishBg_%i.png", i];
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[animationArray addObject:[UIImage imageWithData:imageData]];
What can my problem be? When I reduce the number of frames to about 100, then the animation plays and the app doesn't crash. But when I bring up the frame count to 200, then the app crashes. What's a better way to do this? The animation is a PNG sequence of transparent images, so I'm not sure if I'd be able to convert this to a video and keep its transparency and place other images under it.
Since we need to conserve as much memory as possible here (assuming that’s why you’re crashing), try managing memory more explicitly:
NSString *imageName=[[NSString alloc] initWithFormat:#"fishBg_%i.png", i];
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
[imageName release];
UIImage *theImage = [[UIImage alloc] initWithContentsOfFile:fileLocation];
[animationArray addObject:theImage];
[theImage release];

Help needed in the following code

Can we assign the value of an object of an array having an image value to a variable of image view, see the following code
NSArray *imgArray=[[NSArray alloc] initWithObjects:#"Bingo2.png", nil];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
img.image=[imgArray objectAtIndex:0]; //line 3
[self.view addSubview:img];
its not working, Application is crashing i guess because of line 3
Please help me,
Many Thanks for the help.
You are storing an NSString object in the array and not an image. That is why it is crashing.
use this
img.image= [UIImage imageNamed:[imgArray objectAtIndex:0]];
instead of
img.image=[imgArray objectAtIndex:0];
if the above doesn't work
than you can also use this
NSString *string = [NSString stringWithFormat:#"%#", [imgArray objectAtIndex:0]];
img.image= [UIImage imageNamed:string];
You have a string of a filename in your array, not an image. Use [UIImage imageNamed:#"Bingo2.png"] instead.

sizeWithFont memory leak in iphone

I have this code:
[[data objectForKey:[keys objectAtIndex:0]]
sizeWithFont:[UIFont systemFontOfSize:12]
constrainedToSize:CGSizeMake(276.0, 1000.0)
lineBreakMode:UILineBreakModeTailTruncation];
data is a NSDictionary.
It is said this code has 16 bytes leak, but I cant find it.
Help
What type does the NSDictionary return?
[[data objectForKey:[keys objectAtIndex:0]]
Break the statement up to better figure out where the leak may be:
NSString *s = [[data objectForKey:[keys objectAtIndex:0]];
CGSize size = [s sizeWithFont:[UIFont systemFontOfSize:12]
constrainedToSize:CGSizeMake(276.0, 000.0)
lineBreakMode:UILineBreakModeTailTruncation];
Do you leak only one 16byte block for the entire life of your app? Or are you leaking 16 bytes every time through a loop?
If it is 16 bytes only, I am not sure if I'd worry too much about it. I'm saying that given that some of the caching I have seen done by the OS tends to look like leaking.