I could use a little help on this one. I narrowed the crash source down to NSMutableArray viewControllers by commenting out the release statement in dealloc.
Code:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < self.numSlides; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// Later on in dealloc:
[viewControllers release], viewControllers = nil;
In the main.m file [ int retVal = UIApplicationMain(argc, argv, nil, nil); ] we get:
Program received signal: "EXC_BAD_ACCESS"
When I comment out the release for viewControllers -> no crash but memory leak.
Are you out there Obi-Wan - you are our last hope.
Thanks in advance for the help.
The code you posted looks fine; without seeing any of the rest of it, I’d guess that your viewControllers property is declared as assign rather than retain. If that isn’t it, try running the static analyzer (Product > Analyze or Cmd-Shift-B); it can usually catch that kind of problem.
Related
i dont know what is wrong with code below
#import "ViewController.h"
#interface ViewController ()
{
NSMutableArray * buttons;
NSMutableArray * centers;
int counter;
int index;
}
#end
#implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
buttons = [NSMutableArray new];
centers = [NSMutableArray new];//error here expected identifier or '('
}
//other methods
#end
i am getting two arrays, xcode works normally for buttons but it gives error for centers when creating. What may be the problem?
note: i tried deleting derived data.
Okay, I know this isn't the question you asked, but I would like to suggest an alternative to instantiating variables in viewDidLoad. It's something called "lazy instantiation", and it looks like this:
- (NSMutableArray *)buttons
{
if (!_buttons) _buttons = [NSMutableArray new];
return _buttons;
}
When you need to reset the array, just set it to nil. It won't reallocate the memory until you call it again.
P.S. Whatever you do, don't call self.buttons within that method, or you will create an infinite loop.
Just came across a bug in MapKit, wanted to see what the community is experiencing. When adding a MKUserTrackingBarButtonItem to a UIToolBar, I'm seeing the map is not releasing from memory when switching to another viewController (though I see the dealloc is firing).
Can anyone confirm they are seeing this behavior? My testing shows the map releases properly if I do not add MKUserTrackingBarButtonItem. Using iOS7, testing with Instruments/Leaks.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
MKUserTrackingBarButtonItem *userTrackingBarButtonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];
[userTrackingBarButtonItem setAction:#selector(track:)];
UIBarButtonItem *flexibleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObjects:flexibleSpaceBarButton, userTrackingBarButtonItem, flexibleSpaceBarButton, nil] animated:YES];
[self.navigationController setToolbarHidden:NO animated:NO];
}
MKUserTrackingBarButtonItem and the MKMapView each hold strong references to each other, resulting in a circular retain. This appears to be a bug in MapKit itself, and it's still present in iOS 7.1.
A solution is to add a dealloc to your view controller that contains the MapView:
-(void)dealloc
{
userTrackingBarButtonItem.mapView = nil; // Circular reference bug workaround
}
I need to use some core data managed objects in an NSOperation. The problem is that core data is not thread safe and apparently the object can't be loaded from the new thread. Does anybody know a good tutorial for this? I need the object read only... so the thread will not modify them in any way. Some other, unrelated entities may be added on the main thread while these objects are used in the background, but the background entities don't need to be modified at all..
Hmm seemed I fixed the background running issue, but now the problem is nothing is returned to the delegate... Why? In the thred if I nslog the results are all shown but that call to the delegate never happens
This is the code:
-(void)evaluateFormula:(Formula *)frm runNo:(NSUInteger)runCount{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:2];
NSManagedObjectID *formulaId = frm.objectID;
for (int i = 0; i < runCount; i++) {
NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(runFormula:) object:formulaId];
[queue addOperation:op];
}
}
-(void)runFormula:(NSManagedObjectID *)fId {
NSManagedObjectContext *thredContext =[[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coord = (NSPersistentStoreCoordinator *)[(PSAppDelegate *)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator];
[thredContext setPersistentStoreCoordinator:coord];
Formula *f = (Formula *)[thredContext objectWithID:fId];
NSDictionary *vars = [self evaluateVariables:[f.hasVariables allObjects]];
NSMutableString *formula = [NSMutableString stringWithString:f.formula];
for (NSString *var in [vars allKeys]) {
NSNumber *value =[vars objectForKey:var];
[formula replaceOccurrencesOfString:var withString:[value stringValue] options:NSCaseInsensitiveSearch range:NSMakeRange(0, [formula length])];
}
//parse formula
NSNumber *result = [formula numberByEvaluatingString];
// NSLog(#" formula %# result : %d",formula,[result intValue]);
//aggregate results
[self performSelectorOnMainThread:#selector(aggregate:) withObject:result waitUntilDone:YES]; // the delegate doesn't get called ...
}
-(void)aggregate:(NSNumber *)res {
[self.delegate didReceiveResult:res];
}
This is the follow up to my question earlier about the Xcode 4 static analyzer. It is not specifically a problem since I have the code now working as it needs to, but I am just wondering how things are working behind the scenes. Consider the following code:
- (IBAction)cameraButtonPressed:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
{
return;
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
[self presentModalViewController:cameraUI animated:YES];
NSString *theString = [[NSString alloc] initWithString:#"cameraButtonPressed done"];
NSLog(#"%#", theString);
}
To me, the way this code looks, there are two objects (cameraUI and theString) that need to be released. However, the analyze function correctly identifies that only theString needs to be released at the end of the method, even though both objects are returned from alloc-init, which in my experience has always meant that you release when you are done.
The question I have here is, how does the static code analyzer know not to flag cameraUI as an issue?
I would call this a bug with the static analyzer. The UIImagePickerController instance assigned to cameraUI should be released or autoreleased in a non-garbage-collected environment (like iOS).
I have the following problem making me crazy.
My app has to play a tick sound every second for a specified number of times (e.g. 5) after the user has pressed a button.
I used this:
for (int w=1; w<=5; w++) {
[NSThread detachNewThreadSelector:#selector(tic) toTarget:self withObject:nil];
[NSThread sleepForTimeInterval:1.0];
}
where:
- (void)tic {
NSAutoreleasePool *ticPool = [[NSAutoreleasePool alloc] init];
player_tic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"tick" ofType:#"aif"]] error:nil];
[player_tic setDelegate:self];
[player_tic play];
[ticPool drain];
}
and:
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)player_tic successfully:(BOOL)flag {
NSLog(#"Audio finished playing.");
[player_tic release];
}
All seems to work. My problem is that the object are released together at the end.
I need that after every played sound the related 'player_tic' has to be released immediately.
Other Info:
In .m:
#synthesize player_tic;
In .h:
#interface myController : UIViewController {
...
AVAudioPlayer *player_tic;
}
#property (nonatomic, retain) AVAudioPlayer *player_tic;
On top of all, I have this warning in compilation:
local declaration of 'player_tic' hides instance variable
Please help me!
Thank you very much in advance.
--Carlo
I found System Sound Services more responsive than AVAudioPlayer.
It also solves your release problem, which is caused by the retain property of your controller. Read the reference manual on memory management for further information on retain/release.
I think you want to use self.player_tic when setting that variable, and i believe you also want to autorelease that instance. I'm not sure that setting up a separate NSAutoreleasePool is necessary either.
self.player_tic = [[[AVAudioPlayer alloc] initWithContentsOfURL:... error:nil] autorelease]