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.
Related
I have a block of text that have to be parsed. It is kind a template like
"Dear $name, we need the registration number of your $vehicle, bla bla"...
imagine this 1000 characters long, with a lot of key variables, like $name, $vehicle, etc.
This text is stored on a #define
At run time, I have to parse this template and other 20 like that, replacing the key variables with the real values, like "Dear John, ....".
I was using a NSString variable to store the initial text and then these lines
NSString *start = TEMPLATE1;
start = [start stringByReplacingOccurrencesOfString:NAME withString:realName];
start = [start stringByReplacingOccurrencesOfString:VEHICLE withString:realVehicle];
and so one and the code is working fast and well, but someone suggested using a NSMutableString for the start variable, as it would use less memory.
Is this correct?
Will it worth the change?
It would be reasonable to do this:
NSMutableString *text = [NSMutableString stringWithString:TEMPLATE1];
[text replaceOccurrencesOfString:NAME withString:realName options:0 range:NSMakeRange(0, [text length])];
[text replaceOccurrencesOfString:VEHICLE withString:realVehicle options:0 range:NSMakeRange(0, [text length])];
But if your code is already "working fast and well", I wouldn't bother changing it.
NSString *hostStr2 = [[NSString alloc] initWithFormat:#"http://%#/getplaylist.php?ip=%#",yourip,restip];
NSLog(#"XMLAppDelegate checkstatusthread call to php for status,playlist and nowplaying XML's %#" ,hostStr2);
NSData *dataURL2 =[NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr2 ]];
[hostStr2 release];
NSString *playlistdata=[[NSString alloc] initWithData:dataURL2 encoding:NSASCIIStringEncoding];
NSArray *ipItemsArray;
// memory leak showing at below line
ipItemsArray =[playlistdata componentsSeparatedByString:#"|^|"];
[playlistdata release];
What I am storing in ipItemsArray is a big XML data separated with a delimiter '|^|'.
The problem is when i run this it is giving memory leaks in this array.
Is there any other type of array that we can use or can someone better optimize this code so that i can get rid of the memory leaks happening.
better to allocate memory to that array.And release it in the dealloc a method
I am loading images in UIWebview using the following code. For each image (while scrolling my scrollview) a function is called to load the image. The problem is that when I am continuously scrolling up to 30 images the application crashes. What may be the reason? My images are 1300 by 1200 pixels. Please help me find a solution.
- (void) loadCatalogImage
{
#try{
// if(imgView.image != nil)
// return;
// [global_imgProgress startAnimating];
//NSLog(#"image loading at = %#, %d", baseURL, 2 + pageNo);
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//NSArray *array = [global_ContentString componentsSeparatedByString:#"###"];
NSArray *array1 = [catalogURL componentsSeparatedByString:#"&"];
//NSLog(#"baseURL = %#",baseURL);
NSLog(#"loading catalog image(method: loadCatalogImage).......%#%#",baseURL, [[[array1 objectAtIndex:0] componentsSeparatedByString:#"##"] objectAtIndex:0]);
zoomedImageURL = [NSString stringWithFormat:#"%#%#", baseURL, [[[array1 objectAtIndex:0] componentsSeparatedByString:#"##"] objectAtIndex:1]];
// NSLog(#"Catalog ZOOM URL = %#", zoomedImageURL);//[[array1 objectAtIndex:0] componentsSeparatedByString:#"##"]);//[[[array objectAtIndex:[[global_CatalogRef objectAtIndex:pageNo] intValue]] componentsSeparatedByString:#"##"] objectAtIndex:3]);
[zoomedImageURL retain];
NSLog(#"aaaaaaa = %#",zoomedImageURL);
[webView loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:
[NSString stringWithFormat:#"%#%#",baseURL, [[[array1 objectAtIndex:0] componentsSeparatedByString:#"##"] objectAtIndex:1]
]
]]
];
}
}
Any app that loads up enough resources to occupy so much memory will crash. Apple has several demo projects that deal specifically with loading a large image by breaking it down into smaller bits ci suggest you take a look at them.
Your images are 1300 x 1200 and you're loading 30 of them? You may WILL be running out of memory. If your app is ignoring memory warnings, and hogging up memory, the iOS system may be forcing your app to quit. As I mention in my comment below, that many images, that size, would be about 180megs. Far too big.
Do you really need to deliver such large images to an iphone app, and so many of them?
Can you put a crash log please? If possible Enable the Environment variable 'NSZombieEnabled' and then run the application and have a look at the crash log. This will help us to get the exact problem. By the way why are you creating and retaining "zoomedImageURL"?
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]);
Using objective-c on the iPhone, what is wrong with this code?
Is it leaking memory? Why?
How would I do this correctly?
NSMutableString *result = [NSMutableString stringWithFormat:#"the value is %d", i];
... then later in my code... I might need to change this to:
result = [NSMutableString stringWithFormat:#"the value is now %d", i];
I need to use stringWithFormat a 2nd time... but isn't that creating a NEW string and not correctly freeing the old one?
No, it doesn't leak memory because stringWithFormat: returns an autoreleased object.
You could use the instance method "setString" for your already existing NSMutableString, like this:
[ result setString:[NSString stringWithFormat:#"the value is now %d", i] ];
If you really want to reuse the string, you can use something like
[result setString:#""];
[result appendFormat:#"the value is now %d", i];
However, unless you notice a performance/memory problem, just use
NSString *result = [NSString stringWithFormat:#"the value is %d", i];
/* ... */
result = [NSString stringWithFormat:#"the value is now %d", i];
It's generally easier to work with immutable objects because they can't change under your feet.
What you have seems to me to be the natural way to replace a mutable string with new content, unless you have other references to the same mutable string elsewhere.
If you don't have other references to it and you are reusing the string only to improve performance/memory footprint, that sounds like premature optimisation.
By the way, you do not own a string you obtain via stringWithFormat: so you do not need to (Indeed must not) release it.