I was just wondering if anyone could explain to me why the following lines are showing as having memory leaks within Instruments:
self.videoEngine = [[VideoEngine alloc] initWithCallbackName:#"IntroFinished"];
self.videoEngine = [[VideoEngine alloc] initWithCallbackName:#"MainMovieFinished"];
self.timerMap = [NSTimer scheduledTimerWithTimeInterval:fps target:self selector:#selector(updateAnimationTimer) userInfo:nil repeats:YES];
NSString *locationName2 = [[NSString alloc] initWithString:[locationName substringFromIndex:test]];
Is there an issue with not using a pre-set NSString when initialising? In the examples of self.videoEngine and self.timerMap they both have (nonatomic, retain) properties and are synthesised before use.
If your not using Arc (Which with the retain mentioned, i presume your not) then that will be your memory leak.
When you assign the VideoEngine property it is doing another retain on your object. You need to add autorelease to then end of you alloc statements.
self.videoEngine = [[[VideoEngine alloc] initWithCallbackName:#"IntroFinished"] autorelease];
self.videoEngine = [[[VideoEngine alloc] initWithCallbackName:#"MainMovieFinished"] autorelease];
self.timerMap = [NSTimer scheduledTimerWithTimeInterval:fps target:self selector:#selector(updateAnimationTimer) userInfo:nil repeats:YES];
NSString *locationName2 = [[NSString alloc] initWithString:[locationName substringFromIndex:test]];
Related
I have an app that updates the interface depending on the result of a NSURL request. I have set it up so that the request is fired when my app comes into the foreground, and only if the current view controller is called "ProfileViewController".
My problem is that the interface locks up for a few seconds every time I bring the app back from the background. I am trying to fully understand main/background threads, but am not sure what I can do to make the app remain responsive while the NSURL check is being performed. Any assistance would be great! Thanks!
In my View Did Load Method:
-(void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification
object:nil];
//additional code
}
Then in my App Returns Active Method:
- (void)appReturnsActive {
[myTimer invalidate];
myTimer = nil;
//ONLY WANT TO PERFORM THE UPDATE IF VIEWING THE PROFILE VIEW CONTROLLER
UIViewController *currentVC = self.navigationController.visibleViewController;
NSString * name = NSStringFromClass([currentVC class]);
if ([name isEqualToString:#"ProfileViewController"]) {
[activityIndicator startAnimating];
[activityIndicatorTwo startAnimating];
locationManagerProfile.delegate = self;
locationManagerProfile.desiredAccuracy = kCLLocationAccuracyBest;
[locationManagerProfile startUpdatingLocation];
}
}
Finally, in my Did Update Location Method, I get the distance between the user and the location. If the result is equal to 1, then I update the Interface to show different buttons. This is where the interface freezes up:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[self performSelectorOnMainThread:#selector(buttonUpdate:)
withObject:NULL waitUntilDone:NO];
}
}
New Method:
-(void)buttonUpdate {
NSString *userLongitude =[NSString stringWithFormat:#"%.8f",
currentLocation.coordinate.longitude];
NSString *userLatitude = [NSString stringWithFormat:#"%.8f",
currentLocation.coordinate.latitude];
[locationManagerProfile stopUpdatingLocation];
NSString *placeLatitude = [[NSUserDefaults standardUserDefaults]
stringForKey:#"savedLatitude"];
NSString *placeLongitude = [[NSUserDefaults standardUserDefaults]
stringForKey:#"savedLongitude"];
NSString *distanceURL = [NSString
stringWithFormat:#"http://www.website.com/page.php?
lat1=%#&lon1=%#&lat2=%#&lon2=%#",userLatitude, userLongitude, placeLatitude,
placeLongitude];
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
NSString *distanceInFeet = [[NSString alloc] initWithData:distanceURLResult
encoding:NSUTF8StringEncoding];
if ([distanceInFeet isEqualToString:#"1"])
{
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:#"Button 1"
style:UIBarButtonItemStyleBordered target:self
action:#selector(actionTwo)];
self.navigationItem.rightBarButtonItem = btnGo;
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:#"Button
Two" style:UIBarButtonItemStyleBordered target:self
action:#selector(actionOne)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
}
Your CoreLocation delegate methods (e.g. "didUpdateToLocation", etc.) are all happening in the background, on secondary threads, while everything UI-related needs to happen on the main thread.
To fix your problem, you should modify your UI on the main thread. One of the handy foundation API's you can use is:
performSelectorOnMainThread:withObject:waitUntilDone:
To fix your problem, move your button-creating code into a separate method and call that new method via "performSelectorOnMainThread" from the old (being called on a separate thread via the CoreLocation delegate protocol) and you should be good to go.
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
is a synchronous call. It means your code will wait for the result of the network call to continue. You should make an Asynchronous request, with NSURLConnection (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html) or AFNetworking.
I have a property an NSArray property called toolbarButtons that in one instance (when the UIViewController is modal) it needs to be an NSMutableArray, and when the UIViewController is nonmodal, it needs to simply be an NSArray.
I have initialized the property like this:
#property (nonatomic, copy) NSArray *toolbarButtons;
I am initializing toolbarButtons as follows:
- (void) setToolbarButtons
{
UIBarButtonItem *exitButton = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStyleBordered target:self action:#selector(exitButtonPushed)];
toolbarButtonsTemp = (NSMutableArray *)[[NSMutableArray alloc] init];
[(NSMutableArray*)toolbarButtonsTemp addObject:exitButton];
self.toolbarButtons = toolbarButtonsTemp;
[exitButton release];
}
Then later on I override the above method in a subclass like this:
- (void) setToolbarButtons
{
[super setToolbarButtons];
UIBarButtonItem *leftFlexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *toolButton = [[UIBarButtonItem alloc] initWithTitle:#"Tools" style:UIBarButtonItemStyleBordered target:self action:#selector(toolButtonPushed)];
[(NSMutableArray*)self.toolbarButtons addObject:leftFlexibleSpace];
[(NSMutableArray*)self.toolbarButtons addObject:toolButton];
[toolButton release];
[leftFlexibleSpace release];
}
and I'm getting the following error:
[__NSArrayI addObject:]: unrecognized selector sent to instance
I know there are a ton of work arounds to this, but from a design perspective I'm just wondering if what I'm doing is possible and how to correct my mistake.
#property ( nonatomic, copy ) NSArray* array
means:
- (void) setArray: (NSArray*) array
{
_array = [array copy];
}
[array copy] always returns an non mutable NSArray, even if array is a NSMutableArray.
#property ( nonatomic, copy ) NSMutableArray* array
is handled a bit differently:
- (void) setArray: (NSMutableArray*) array
{
_array = [array mutableCopy];
}
In that case, you can do
self.array = (NSMutableArray*) [NSArray new];
and array will be assigned with a NSMutableArray, as mutableCopy always returns an NSMutableArray.
The latter is of course strongly discouraged. It works for now. No guarantee it will work forever.
Your other choice, besides an NSMutableArray, is to use a strong property and to handle the copies yourself.
you cannot do that!
when you:
[(NSMutableArray*)self.toolbarButtons addObject:leftFlexibleSpace];
[(NSMutableArray*)self.toolbarButtons addObject:toolButton];
you are just casting the property, it means you are promising in runtime that that property is a NSMutableArray... but you are lying!!!
i suggest you do the contrary: just declare it as a NSMutableArray (it's a subclass of NSArray, so you can use it as a NSArray when you want it),and use the NSMutableArray methods just when you need them
Casting won't work in this case. You need to change your property to an NSMutableArray:
#property (nonatomic, copy) NSMutableArray *toolbarButtons;
You should just use an NSMutableArray in your property, but you could always do something like this too...
NSMutableArray *mutArray = [[NSMutableArray alloc] init];
[mutaArray addObject:leftFlexibleSpace];
[mutaArray addObject:toolButton];
self.toolbarButtons = [NSArray arrayWithArray:mutArray];
What is wrong with this code?
in the interface:
NSArray *myImages;
#property (nonatomic, retain) NSArray *myImages;
implementation:
NSArray *array = [NSArray arrayWithObjects:
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image2.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image3.png"]],
nil];
self.myImages = array;
[array release];
If I log myImages right after initializing it, it correctly logs the array of UIImageViews. However, later in the app, when I try to access self.myImages from a different method, I get EXC_BAD_ACCESS. It is getting retained in the interface. What is the problem?
Do not release array. Using arrayWithObjects:, it will return an autoreleased object. In a sense, you are releasing it twice. An alternative is:
[[NSArray alloc]initWithObjects:...]
Then you can release array.
See Apple's memory management article:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html%23//apple_ref/doc/uid/20000043-BEHDEDDB
arrayWithObjects is a convenience method and returns an autoreleased object, so remove the
[array release];
Plus you leak memory by doing this :
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]]
Because this time the imageView isn't released.
arrayWithObjects returns an autoreleased object, you're over releasing it. See here http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH
I have a question about reference counting.
This is my constructor:
- (id)initWithId:(NSString *)graphId;
In another class I make an object in the following way:
GraphViewController *graph =
[[GraphViewController alloc] initWithId:[[NSString alloc] initWithFormat:#"%s", current_ID];
My question is: how do I correctly release the string object?
Is it correct to release the string passed as a parameter?
Any of these ways will work:
(1)
GraphViewController *graph = [[GraphViewController alloc] initWithId:
[[[NSString alloc] initWithFormat:#"%s", current_ID] autorelease]];
(2)
GraphViewController *graph = [[GraphViewController alloc] initWithId:
[NSString stringWithFormat:#"%s", current_ID]];
(3)
NSString *aString = [[NSString alloc] initWithFormat:#"%s", current_ID];
GraphViewController *graph = [[GraphViewController alloc] initWithId: aString];
[aString release];
And, of course, graph needs to be released or autoreleased somewhere.
Never pass ownership around. I.e., in your call to initWithId: you create a string. Make sure it's released in the exact same place. Do not hand the ownership over, burdening another function with the task of releasing what you created (or retained, ...).
If you're not calling initWithId: an unusually high number of times per second, do an autorelease. Even better, use a convenience function. Do not let people talk you into "avoiding the autorelease pool". Because a) it's not so easy to avoid and b) you have more important things to worry about.
In code:
NSString* graphID = [NSString stringWithFormat: #"%s", current_ID];
GraphViewController* graph = [[[GraphViewController alloc] initWithId: graphID] autorelease];
In the constructor, you will simply retain the ID and in the dealloc you will release it again. And please... use (private) #property declarations for this. You can then completely remove the ivar "graphID" from your public interface (if it's private, of course).
#interface GraphViewController ()
#property (copy, nonatomic) NSString* graphID; // copy instead of retain for potentially mutable objects
#end
Then, in initializer and dealloc, boilerplate stuff like:
#synthesize graphID;
- (id)initWithId:(NSString*) ID;
{
self = [super init];
self.graphID = ID;
return self;
}
- (void) dealloc
{
self.graphID = nil; // controversial, Apple policy is [graphID release]; graphID = nil;
[super dealloc];
}
Using this style will make you sleep better at night. Delighted users will raise statues in your name.
Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the sound that i want it to.
Now the problem is with the timer. I want the timer to start on the click on the same button, and the timer value will be displayed in a label. I am not very clear with the NSTimer itself either yet. I guess i am doing something wrong here. Can anyone help me with this.
-(IBAction)playSound { //:(int)reps
NSString *path = [[NSBundle mainBundle] pathForResource:#"chicken" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
theAudio.delegate = self;
[theAudio play];
[self startTimer];
}
- (void)startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(targetMethod) userInfo:nil repeats:YES];
labelA.text = [NSString stringWithFormat:#"%d", timer];
}
Using the code above, when i click on the button, it plays the sound, and then my app closes.
Thanks
Zeeshan
This line:
labelA.text = [NSString stringWithFormat:#"%d", timer];
makes absolutely no sense. The timer will call the method you specify as the selector in scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: when it fires so you have to implement that method and update your label there. The first line of startTimer is almost correct, but the selector must include a colon (because it denotes a method that takes one parameter):
- (void)startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerFired:) userInfo:nil repeats:YES];
}
Note that I named the selector timerFired: so we have to implement that method. If you want the timer to increment a counter, you will have to do that in this method, too:
- (void)timerFired:(NSTimer *)timer {
static int timerCounter = 0;
timerCounter++;
labelA.text = [NSString stringWithFormat:#"%d", timerCounter];
}
Don't forget to invalidate the timer later when you no longer need it.