I am trying to learn a bit about the instruments panel in Xcode. I am using ARC. When checking allocations I have been coming up with some abandoned memory it issues. The picture below depicts code from my prepareForSegue method. Every time I tap to perform the segue, memory is allocated and never released. So if I go back and forth between the two viewControllers, the memory keeps climbing and eventually terminates the app. I used the tools to narrow down the problems in the code but am unsure where to go from here. I have seen several examples online about how to identify the problem but not about how to resolve it. So my question is, what is going on here that is causing the app to not release the memory?
properties for viewController 1:
#property (nonatomic, retain) NSMutableArray *recAlbumsArray;
#property (nonatomic, retain) NSMutableArray *titlesArray;
#property (nonatomic, retain) NSMutableArray *timersArray;
#property (nonatomic, retain) NSMutableArray *albumsArray;
#property (nonatomic, retain) NSMutableArray *imagesArray;
#property (nonatomic, retain) NSMutableArray *objsIdArray;
#property (nonatomic, retain) NSArray *imagesDataArray;
#property (nonatomic, weak) NSArray *recAlbumData;
#property (nonatomic, weak) NSArray *albumData;
#property (nonatomic, weak) NSString *albumTitle;
#property (nonatomic, weak) NSString *objId;
properties for viewController 2:
#property (nonatomic, retain) NSMutableArray *album;
#property (nonatomic, weak) NSString *title;
#property (nonatomic, weak) NSString *albumRef;
#property (nonatomic, weak) NSString *objId;
I have tried setting all the properties to nil in a viewDidDisappear method and also tried removing the 1st viewController from the viewController array hierarchy but nothing changed.
Related
I am facing a very weird issue. I create an object and I add it to a NSMutableArray but when I try to read it after I insert it, some subclasses of the object change to some weird classes like
PINGIFAnimatedImageManager
Here is the code I use to create the object and insert it to the NSMutableArray:
CustomContentGridRow *row = [[CustomContentGridRow alloc]init];
row.child1 = [dataManager getMapLocation]; // This is the MapLocation object that will change to this weird PINGIFAnimatedImageManager
row.useFullWidth=1;
row.index=0;
[arrCustomChilds addObject:row];
This is the CustomContentGridRow class:
#interface CustomContentGridRow : NSObject
#property (nonatomic, assign) MapLocation *child1;
#property (nonatomic, assign) MapLocation *child2;
#property (nonatomic, assign) int useFullWidth;
#property (nonatomic, assign) int index;
#end
So when I put a breakpoint at this line [arrCustomChilds addObject:hotelRow];, when I read the row object I get the expected results. But when I place a breakpoint after the above code to read the arrCustomChilds the class of child1 changes to some weird classes. Also, sometimes it won't change to another class but it will give nil values.
Any idea why this is happening?
You should change property modifier from "assign" to "strong" for class objects. Otherwise undefined behaviour can happen.
In Xcode -> Product -> Scheme - edit Scheme. Check the settings of RUN mode. If it is Release change to Debug.
This will give you the correct values
Change below properties
#property (nonatomic, strong) MapLocation *child1;
#property (nonatomic, strong) MapLocation *child2;
assign to strong
Your interface should be:
#interface CustomContentGridRow : NSObject
#property (nonatomic, strong) MapLocation *child1;
#property (nonatomic, strong) MapLocation *child2;
#property (nonatomic, assign) int useFullWidth;
#property (nonatomic, assign) int index;
#end
the class objects should be strong not assign
I'm having an issue with a memory leak. In Instruments, I'm getting a memory leak on this line:
VDLPlaybackViewController *videoController = [[VDLPlaybackViewController alloc] initWithNibName:#"VDLPlaybackView" bundle:nil];
I'm not sure what could be the issue on this. Here is the header file for VDLPlaybackViewController.h:
#protocol VDLPlaybackViewControllerDelegate <NSObject>
#required
-(void)playerShouldClose;
#end
#interface VDLPlaybackViewController : UIViewController <UIDocumentInteractionControllerDelegate>
#property (nonatomic, strong) id<VDLPlaybackViewControllerDelegate> delegate;
// content file stuff.
#property (nonatomic, strong) File *file;
#property (nonatomic, strong) NSNumber *contentID;
#property (nonatomic, strong) NSURL *fileURL;
#property (nonatomic, strong) NSString *pageTitle;
// layout stuff
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *topTimeViewHeight;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomControlViewHeight;
#property (nonatomic, strong) IBOutlet UIView *movieView;
#property (nonatomic, strong) IBOutlet UIView *navBarView;
#property (nonatomic, strong) IBOutlet UISlider *positionSlider;
#property (nonatomic, strong) IBOutlet UIButton *timeDisplay;
#property (nonatomic, strong) IBOutlet UIButton *playPauseButton;
#property (nonatomic, strong) IBOutlet UIButton *subtitleSwitcherButton;
#property (nonatomic, strong) IBOutlet UIButton *audioSwitcherButton;
#property (nonatomic, strong) IBOutlet UIButton *screenSizeSwitcherButton;
//#property (nonatomic, strong) IBOutlet UINavigationBar *toolbar;
#property (nonatomic, strong) IBOutlet UIView *controllerPanel;
#property (nonatomic, strong) IBOutlet UISlider *volumeSlider;
#property (nonatomic, strong) IBOutlet MPVolumeView *volumeView;
#property (nonatomic, strong) IBOutlet MPVolumeView *airplayView;
#property (nonatomic, assign) CGRect shareButtonFrame;
#property (nonatomic, strong) MultiFileShareButtonController *shareButtonController;
#property (nonatomic, strong) FavoriteFileButtonView *favoriteButtonController;
#property (nonatomic, strong) UIDocumentInteractionController *interactionController;
#property (nonatomic, assign) BOOL isDestroying;
- (void)setMediaURL:(NSURL*)theURL;
- (IBAction)closePlayback:(id)sender;
- (IBAction)positionSliderDrag:(id)sender;
- (IBAction)positionSliderAction:(id)sender;
- (IBAction)toggleTimeDisplay:(id)sender;
- (IBAction)playandPause:(id)sender;
//- (IBAction)switchAudioTrack:(id)sender;
//- (IBAction)switchSubtitleTrack:(id)sender;
- (IBAction)switchVideoDimensions:(id)sender;
#end
Does anyone know what causes this?
The instrumentation tool tells you that there is a leak on the following line:
VDLPlaybackViewController *videoController = [[VDLPlaybackViewController alloc] initWithNibName:#"VDLPlaybackView" bundle:nil];
It does not mean, that it's just this line which is causing the leak, the tool is telling you that a reference of this variable exists as a leak.
You should look for instances where a strong reference to VDLPlaybackViewController *videoController was passed.. may be as delegates or in completion blocks.
The interface which you have in the question has a little problem. It should be weak instead of strong
#property (nonatomic, weak) id<VDLPlaybackViewControllerDelegate> delegate;
Find more instances like this, where you've passed around VDLPlaybackViewController as a strong reference delegate and you would be able to resolve the problem.
To understand more on why the leak is actually happening. Go through
https://cocoacasts.com/what-are-strong-reference-cycles
I have one problem in converting JSON array to model. I am using JSONModel library.
#protocol PTTemplateModel <NSObject>
#end
#protocol PTProfileTemplateModel <PTTemplateModel>
#end
#protocol PTCategoryTemplateModel <PTTemplateModel>
#end
#interface PTTemplateModel : JSONModel
#property (nonatomic, assign) TemplateType type;
#property (nonatomic, copy) NSString* templateID;
#end
#interface PTProfileTemplateModel : PTTemplateModel
#property (nonatomic, copy) NSString* logoURL;
#property (nonatomic, copy) NSString* title;
#end
#interface PTCategoryTemplateModel : PTTemplateModel
#property (nonatomic, strong) NSString* category;
#end
#interface PTModel : JSONModel
#property (nonatomic, copy) NSString* title;
#property (nonatomic, strong) NSArray< PTTemplateModel>* templates; // PTTemplateModel
Here templates array can have both PTProfileTemplateModel and PTCategoryTemplateModel.
JSON Input:
{"title":"Core","templates":[{"type":0,"templateID":"","logoURL":"", "title":"data"},{"type":1,"templateID":"","category":"DB"}]}
What I need is according to type I have to get CategoryTemplate or ProfileTemplate. But after conversion I am getting just PTTemplateModel type.
I know that I have specified protocol type as PTTemplateModel. But how do I get different type of model according to given data.
I tried:
#property (nonatomic, strong) NSArray< PTTemplateModel>* templates;
#property (nonatomic, strong) NSArray<PTProfileTemplateModel, PTCategoryTemplateModel>* templates;
#property (nonatomic, strong) NSArray< PTTemplateModel , PTProfileTemplateModel, PTCategoryTemplateModel>* templates;
None of them works.
Any suggestions?
Why not try BWJSONMatcher, it helps you work with json data in a very very neat way:
#interface PTModel : NSObject<BWJSONValueObject>
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSArray *templates;
#end
#interface PTTemplateModel : NSObject
#property (nonatomic, assign) TemplateType type;
#property (nonatomic, strong) NSString *templateID;
#property (nonatomic, strong) NSString *logoURL;
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSString *category;
#end
In the implementation of PTModel, implement the function typeInProperty: declared in protocol BWJSONValueObject:
- (Class)typeInProperty:(NSString *)property {
if ([property isEqualToString:#"templates"]) {
return [PTTemplateModel class];
}
return nil;
}
Then you can use BWJSONMatcher to get your data model within one line:
PTModel *model = [PTModel fromJSONString:jsonString];
Detailed examples of how to use BWJSONMatcher can be found here.
I'm developing a chat app and I'm using Parse for backend.
I have a Discussion table to save Discussion between 2 user.
#interface Discussion : PFObject <PFSubclassing>
#property (nonatomic, retain) PFUser * customer;
#property (nonatomic, retain) PFUser * creator;
#property (nonatomic, retain) Quote * quote;
#property (nonatomic, retain) NSDate * lastMessageTime;
#property long messageCount;
#end
I have a Message table to save message.
#interface Message : PFObject <PFSubclassing>
#property (nonatomic, retain) PFUser * sender;
#property (nonatomic, retain) Discussion * discussion;
#property (nonatomic, retain) NSString * content;
#property (nonatomic, retain) PFFile * image;
#property (nonatomic, retain) PFFile * imageThumb;
#property (nonatomic, retain) PFFile * video;
#property (nonatomic, retain) PFFile * videoThumb;
#property (nonatomic, retain) PFGeoPoint * location;
#property (nonatomic, retain) PFFile * sound;
#end
When user chatting, I use cloud code to update lastMessageTime and messageCount.
I want to show a list of people with small label that shows messageCount, and I have a NSTimer to call it automatically each 3.0 seconds.
But when I get new Dicussion, its estimatedData and serverData do not matchs.
I have no idea about this. Please give me your advice.
I have same problem when working on Android Parse SDK.
I think you have edited messageCount on both server side via Cloud Code script and client side via Object-C.
To fix it, you must edit only on client side or server side and sync to another side.
Thanks.
I keep getting random crashes saying:
Received memory warning.
(lldb)
Now after a bit of reading I have found that this is probably due to memory management, resources been fully used and none free. I thought in ARC we dont need to free up memory and release things (it wont even let us release) I thought it did it all by itself.
I have seen from some articles & threads that a possible problem is way that you define #properties so some I have:
FirstViewController
#property (strong) FilterViewController *filterViewController;
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property(nonatomic, retain) IBOutlet UILabel *sliderValue;
#property(nonatomic, retain) NSString *passedData;
#property int selectedTime;
FilterViewController
#property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
#property (strong, nonatomic) IBOutlet UILabel *stepperValueLabel;
#property (strong) FirstViewController *firstViewController;
Your problem is Retain cycle. firstViewController object is retains filterViewController, and filterViewController object is retains firstViewController
#property (strong) FirstViewController *firstViewController; in FilterViewController
#property (strong) FilterViewController *filterViewController; in FirstViewController