ios memory leak - iphone

I have a method that sets up NSXMLParser with the correct data that I am pretty sure is causing a memory leak.
This is my first time using instriments to try and solve a memory leak and have got it down to the offending method..
by looking at the response caller value in instruments memory leak testing.
I am hoping you can tell me where the memory leak is here... because I release both myDataArray and parser... im just not sure where I am going wrong.. do I need to assign them nil also?
- (void)startTheParsingProcess:(NSData *)parserData
{
[myDataArray release]; // clears array for next time it is used.
myDataArray = [[NSMutableArray alloc] init];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
[parser release];
}
any help would be appreciated.
UPDATE:
I have made the changes to my application and when I run it on the simulator I do not get any memory leak errors..
however if I run it on my phone get this in the console...
2011-10-19 11:22:05.673 code[1299:707] -[__NSCFType section]: unrecognized selector sent to instance 0x1b9b80
2011-10-19 11:22:05.683 code[1299:707] CoreAnimation: ignoring exception: -[__NSCFType section]: unrecognized selector sent to instance 0x1b9b80
2011-10-19 11:22:07.949 code[1299:707] -[__NSCFType row]: unrecognized selector sent to instance 0x1b9b80
2011-10-19 11:22:07.951 code[1299:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType row]: unrecognized selector sent to instance 0x1b9b80'
*** First throw call stack:
(0x35e9e8b3 0x366e61e5 0x35ea1acb 0x35ea0939 0x35dfb680 0x334a76cf 0x3353c713 0x30fd5 0x3352cd69 0x335a60ab 0x35cc32ab 0x35e72a57 0x35e726bd 0x35e71293 0x35df44e5 0x35df43ad 0x30fa4fed 0x334a7dc7 0x272f 0x26d8)

Try putting this in the .h
#property (nonatomic, retain) NSMutableArray *myDataArray
.m
#synthesize myDataArray
in the method...
- (void)startTheParsingProcess:(NSData *)parserData
{
self.myDataArray = [NSMutableArray arrayWithCapacity:8];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
[parser release];
}

Related

can't figure out unrecognized selector sent to instance for XMLparser

I had a function XMLParser working but I'm trying to expand the class to handle different XML files my app needs.
I'm getting the error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XMLParser initXMLParserForValidation]: unrecognized selector sent to instance 0x7564ea0'" Here's the code there.
- (void)validateEmail:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
XMLParser *parser = [[XMLParser alloc] initXMLParserForValidation];
[nsXmlParser setDelegate:parser];
BOOL wasSuccessful = [nsXmlParser parse];
if (wasSuccessful) {
self.result = [parser result];
}
}
I've put breakpoints and stuff and but doesn't even get into my initXMLParserForValidation class. Here it is anyway, though.
- (XMLParser *) initXMLParserForValidatation {
self = [super init];
_result = [[ValidationResult alloc] init];
return self;
}
I've tried to mimic the code that's working but I can't see any differences. Driving me nuts. I'm new at this iphone stuff, though. Help much appreciated.
There's a spelling error in the declaration of your class' init method:
initXMLParserForValida*ta*tion
Then you're calling the init method like this, with the proper spelling, which doesn't exist:
initXMLParserForValidation
Remove the extra ta and you should be good to go!

viewDidLoad using Persistence

I have problem with my Iphone project in viewDidLoad event the app crash on
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
I am trying to store information from text Filed can someone help me to solve the problem
- (void)viewDidLoad{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
for (int i = 0; i < 2; i++) {
UITextField *theField = self.lineFields[i];
theField.text = array[i];
}
NSData *data = [[NSMutableData alloc]
initWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
BIDThreeLines *threelines = [unarchiver decodeObjectForKey:kRootKey];
[unarchiver finishDecoding];
for (int i = 0; i < 2; i++) {
UITextField *theField = self.lineFields[i];
theField.text = threelines.lines[i];
}
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
}
Error
2013-03-25 23:29:45.592 MobilePaymentsApp[1182:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d0e8d0
2013-03-25 23:29:45.593 MobilePaymentsApp[1182:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d0e8d0'
*** First throw call stack:
(0x1c96012 0x10d3e7e 0x1d214bd 0x1c85bbc 0x1c8594e 0x1c0ae18 0xb030e8 0x339c 0xf91c7 0xf9232 0x483d5 0x4876f 0x48905 0x51917 0x2cc5 0x15157 0x15747 0x1694b 0x27cb5 0x28beb 0x1a698 0x1bf1df9 0x1bf1ad0 0x1c0bbf5 0x1c0b962 0x1c3cbb6 0x1c3bf44 0x1c3be1b 0x1617a 0x17ffc 0x29fd 0x2925)
libc++abi.dylib: terminate called throwing an exception
(lldb)
https://github.com/a-elnajjar/MobilePaymentsApp
NSKeyedArchiver returns object that you have stored in it. eg. if you have stored an array then it will return an array. so be careful while unarchiveing objects.
in following example i have read an array from NSKeyedUnarchiver.
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Look in the crash log's stack trace to see where exactly this call is
happening.
If the variable you're sending -row to isn't actually typed as an
NSArray, it's likely that you've failed to follow the memory
management rules for that variable. These same symptoms are very
commonly caused by that. Something that responds to -row could have
existed at one point, been deallocated because you didn't -retain it,
and then an NSArray was later allocated in that spot.
Run a "Build & Analyze," and re-re-review the memory management
guidelines until you know them in your sleep.
Source: [NSCFArray row]: unrecognized selector sent to instance 0x3953a20

Crash when using URL for swapping images

#implementation SlideShowViewController
- (id)init
{
NSString *temp = [NSString alloc];
[temp stringwithString:#"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg"];
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
nil,
(CFStringRef)temp,
NULL,
NULL,
kCFStringEncodingUTF8)
autorelease];
NSData *dato = [NSData alloc];
dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
if (self = [super initWithNibName:nil bundle:nil])
{
NSArray * images = [NSArray arrayWithObjects:[UIImage imageWithData:dato],[UIImage imageWithData:dato], [UIImage imageWithData:dato], [UIImage imageWithData:dato], [UIImage imageWithData:dato], nil];
self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];
}
return self;
}
I used the following code to load images from the server and view it as that of a photo album
But when the code is run it gets crashed
the error message in console is as follows
2011-06-24 23:54:01.837
SlideShow[13654:207] *
-[NSPlaceholderString stringwithString:]: unrecognized
selector sent to instance 0x49117e0
2011-06-24 23:54:01.839
SlideShow[13654:207] Terminating
app due to uncaught exception
'NSInvalidArgumentException', reason:
'** -[NSPlaceholderString
stringwithString:]: unrecognized
selector sent to instance 0x49117e0'
2011-06-24 23:54:01.840
SlideShow[13654:207] Stack: (
42178640,
43336492,
42187355,
41649782,
41646578,
12567,
7791,
2906510,
2910543,
2936126,
2917623,
2949592,
51171708,
41457820,
41453736,
2908705 ) terminate called after throwing an instance of 'NSException'
it works if the URLS are replaced by the images
could any one help me
I'm a beginner so its hard for me to find it out
thanks
You are trying to call an NSString class method with an instance (an incorrectly created instance at that) here
NSString *temp = [NSString alloc];
[temp stringwithString:#"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg"];
Change to
NSString *temp = #"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg";
EDIT:
You are doing several things wrong like calling alloc on things and then setting them to something else. (*temp and *data) when you alloc something it should always be followed with a call to init or initXXXX. Next you do not even need those alloc calls because you are setting the pointer to something else on the line right beneath it which causes a memory leak.
This is all you need
NSData *dato = [NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
Then you are creating a bunch of images with the same data object. You are also blocking the calling thread while you are downloading the image which should done later probably around time of viewDidLoad asynchronously.
The init function of the view controller is not the place for setting the view. Implement loadView and the system will call it when it is needed to minimize the applications memory footprint.

MWFeedParser stringByReplacingXmlEntities memory leak

NSURL *xmlUrl = [[NSURL alloc] initWithString:#"http://www.xml-document.xml"];
NSString *converted = [[NSString alloc] initWithContentsOfURL:xmlUrl encoding:NSISOLatin1StringEncoding error:nil];
converted = [converted stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
converted = [converted stringByDecodingXMLEntities];
The last line takes up 98.3% of the memory in Instruments > Leaks.
And it's smashing my Log window with:
__NSAutoreleaseNoPool(): Object 0x6d10ba0 of class UIView autoreleased with no pool in place - just leaking
Why? I think that method has worked good before...
After some more googling I found that it has to be because of these methods:
[self performSelectorInBackground:#selector(load) withObject:loadingIndicator];
[self performSelectorInBackground:#selector(getEvents) withObject:nil];
So I've tried allocating an NSAutoReleasePool and the release it after the work is done in the methods.
Now i receive EXC_BAD_ACCESS message.
This did not happended before I decided to run those methods in the background. So what's the problem here?

iPhone app quits randomly when accessing a NSString

I've got a problem with a NSString in my app.
I've defined it in the header file of my view controller.
NSString *locationCoordinates;
and I set its value in a -(void) method.
- (void)locationUpdate:(CLLocation *)location {
<...>
NSArray *locArray = [locString componentsSeparatedByString:#", "];
NSString *xCoordinate = [locArray objectAtIndex:0];
NSString *yCoordinate = [locArray objectAtIndex:1];
locationCoordinates = [NSString stringWithFormat:#"%#,%#", xCoordinate, yCoordinate];
}
In this method, I can print it to the console with
NSLog(locationCoordinates);
But if I want to view it in the console in another method, my app instantly quits.
- (IBAction)saveAndReturnToRootView {
NSLog(locationCoordinates);
}
The console tells me:
2010-02-24 14:45:05.399 MyApp[73365:207] *** -[NSCFSet length]: unrecognized selector sent to instance 0x4c36490
2010-02-24 14:45:05.400 MyApp[73365:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFSet length]: unrecognized selector sent to instance 0x4c36490'
2010-02-24 14:45:05.401 MyApp[73365:207] Stack: (
32887899,
2434934025,
33269819,
32839286,
32691906,
32417461,
32527181,
32527085,
32747749,
356942,
630491,
63461,
2868313,
4782069,
2868313,
3275682,
3284419,
3279631,
2973235,
2881564,
2908341,
40984273,
32672640,
32668744,
40978317,
40978514,
2912259,
9744,
9598
)
How can I solve this problem?
Thanks in advance ;-)
You arent retaining the String, so the memory is being cleared up. This causes the crash when you try to access it.
To retain it, you can add the following line
[locationCoordinates retain];
Remember to release it when you no longer need it - probably in the destructor of your class, or else you will have a memory leak.
It is standard practice in Objective C to use properties for such class members. In the header file use
#property (nonatomic, retain) NSString *locationCoordinates;
Then in the implementation chuck in a
#synthesize locationCoordinates;
When you access locationCoordinates access it through self as :
self.locationCoordinates = [NSString stringWithFormat:#"%#,%#", xCoordinate, yCoordinate];
Objective C will create a getter and setter property that will handle the retain for you in the most efficient manner.
Incidentally, the nonatomic keyword in the property tells objective c that you dont need it to create any thread synchronization around the property access. If you are going to be multithreading the class, you should consider removing nonatomic. This will then ensure the property access is thread safe.
No point doing any work that you can get the compiler to do for you!
You should retain the string when storing it in a variable of your class:
locationCoordinates = [NSString stringWithFormat:#"%#,%#", xCoordinate, yCoordinate];
[locationCoordinates retain];
The reason is that [NSString stringWithFormat:...] returns an autoreleased instance. The string will be automatically released when the function ends.
You could also copy the string:
locationCoordinates =
[[NSString stringWithFormat:#"%#,%#", xCoordinate, yCoordinate] copy];
And of course, don't forget to release it again in dealloc:
- (void) dealloc {
[locationCoordinates release];
[super dealloc];
}