Abandoned Memory, Leaks, Navigation Controller - objective-c

I can't understand this behaviour in my app and I'm seriously thinking burn my mac NOW and start writing books of stories for children.
I have a normal Navigation Controller, and i push and pop viewcontrollers from it.
I'm doing a basic transition between views and I'm realising everything that I'm retaining or copying or whatever. But Instruments STILL identifies abandoned memory, and says that line is the responsible for that.
I can't understand. What I'm forgetting?
In Area2 I have few outles and one UIImageVIew, do I need to realising them to, in some way?
One more thing, if use Leaks inspector that same line is identified as a leak.. Really, I'm capable to destroy de entire world!
Please, can someone help me and tell me what I'm doing wrong and / or forgettting?
( Iniatly I had self.navigationController instead of delegate.navigationController, but the problems occurs to! )
Just this:
#import
#interface Area2 : UIViewController
{
IBOutlet UIButton * btBack;
}
# pragma mark - Navigation Controller
- (IBAction)goBack:(id)sender;
- (IBAction)goGaleria:(id)sender;
When I do Analyze ( Menu: "Product"->"Analyze" Xcode 4.2 ) give me build succeeded!
UPDATE:
SOLUTION is in the replies at #mit3z's answer! #babbidi answer!

If you have your outlets as properties and you don't release them in dealloc - then yes, you'll get a leak. It's hard to tell without seeing Area2 #interface declaration.

Related

Error loading .xib - application crash

I am banging my head against a brick wall. I am trying to push a view onto a UINavigationController in the usual manner:
[[self navigationController]pushViewController:vc animated:YES];
When I do this the app crashes with the following error:
__CFStringEncodeByteStream + 17
Thread 1: EXC_BAD_ACCESS (code=2,address=xxxxx)
Now, I have replaced the ViewController in question with a template provided by XCode and I do not get the error so the issue must be with my UIViewController class. Furthermore, if I replace the nib name with that of the template UIViewController class it works .i.e.
MyViewController *myVc = [[MyViewController alloc]initWithNibName:#"XCodeViewController" bundle:nil];
This leads me to think that the issue is actually with the nib and not the class itself. I have checked through all the connections and there are no errors or warnings.
Hopefully somebody can help.
Thanks
I was dealing with a very frustrating bug similar to yours, and everything was in place.
The newly created xib was added to the Bundle resources, no nil pointers or released pointer, NSZombieEnabled was set. The File's Owner was set to the correct ViewController as well
After sitting down with my senior for 30 minutes, we finally realized what was wrong -
In the newly created Xib, even after you assign the File's Owner to the ViewController there is an additional step which you need to take.
You need to have the main View's referencing Outlet set to the File's owner as well. The easiest way to do this is to Drag the "+" sign on the referencing outlet(From the Connections inspector) to the "File's Owner" in the Document Outline. After this you just need to click on "view" which pops up and you should be good.
Hope that helps you or anyone else facing the same problem
Need more info to answer, I can give few possibilities.
Check if the class name is mapped properly in your identity inspector.
And check your connections too. If your view is mapped in xib., etc.
If your running the app in OS below 6.0 & built with features such as Auto layout, it leads to crash.
There are multiple possibilities.
Hope this gives an insight.
Check if your nib is added to the application bundle. You can do that by selection you project file->Build Phases->Copy Bundle Resources.
If it is not there, add it.
I had the same issue yesterday and this was the problem.
I hope it helps.

UITableViewController Files

Following a tutorial exactly, I created files that extend UITableViewController. The problem is that his uitableviewcontroller.m files is filled with pre written code (like the viewDidLoad), while mine is completely blank! Both our uitableviewcontroller.h files all have the code of
#import <UIKit/UIKit.h>
#interface ChemTable : UITableViewController
#property (strong,nonatomic)NSMutableArray *Chemarray;
#end
For learning purpose the auto-generated method are of least use(its perfectly ok even if You remove it). even you can create app without them....."viewDidLoad" is the one very necessary method that runs when the view is loaded, but when you go for real apps you will surely use some of auto-generated methods.
Extra -->I think you also also should see this:
ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work
ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen
ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API.
ViewWill/DidDisappear - Same idea as the WillAppear.
ViewDidUnload/Dispose - Available to you, but usually not necessary in Monotouch. In objective-c, this is where you do your cleanup and release of stuff, but this is handled automatically so not much you really need to do here.

UIScrollView does not scroll when an object is inside

I made a UIScrollView inside a UIViewController I have like 5 ViewControllers my problem is in the 4th one.
The UIScrollView I made does scroll when I put notting it it (so no label no button, no objects at all) but when I put even a label (or anything) in the UIScrollView it stops working.
Edit:
I have try'd making the same thing in a new project, for some reason it does work I think it has something to do with the fact that it is in the 4th ViewController and the new one I made, was made, in the First automatically made viewcontroller.
I do not yet have a answer please help.
My code:
viewcontroller.h
#import <UIKit/UIKit.h>
#interface viewcontroller : UIViewController
#property (weak, nonatomic) IBOutlet UIScrollView *ScrollerMdon;
#end
viewcontroller.m
#synthesize ScrollerMdon;
- (id)initWithNibName:(NSString *) nibNameOrNil bundle:(NSBundle *) nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
}
return self;
}
-(void)viewDidLoad
{
[ScrollerMdon setScrollEnabled:YES];
[ScrollerMdon setContentSize:CGSizeMake(320,1000)];
[super viewDidLoad];
}
#end
In xcode viewcontroller connection inspector my outlet ScrollerMdon is connected with *Scroll View(*which I inserted in my viewcontroller)
In xcode UIScrollView connection inspector my Referencing outlet ScrollerMdon is connected with viewcontroller.
Please help me I have been trying to solve this for 5 hours.. I can't stand it anymore.
Edit:
I try to make it possible for a single viewcontroller to hold like 17 textboxes I see no other way than doing it with a UIScrollView if there are any suggestions I would be really happy.
Edit2:
I have try'd to make it all aggain putting the same class on a different view controller and the other way around, I kinda rewrote the code and everything it's exactly the same as what I made in the other project but it does not work.(when an object is inside..)
2 foto's of my program.
Srry for the small and bad images, for some reason they moved without my permision but it is enough information I guess.(I use VMWare 8 with os x leopard on it)
foto 1
foto 2
Me Scrolling without items:
imgur.com/cd16I
Me desperately Scrolling with items:
imgur.com/hNUl1
Since I can only post 2 hyperlinks you have to open these yourself.
PS. please upvote this if you do not know the answer for I need a answer and there will probably be more viewers if it gets upvoted, ty.
Edit3:
I am now copying my entire code and build of the app in a different project hope this wil work :S.
I had the SAME exact problem that I ran into yesterday. Drove me nuts for hours... This was a brand new project that I just started like a week or so ago.
The thing is, I had another project that had the exact same setup essentially that I had started last summer that has no problems. Tabbed application, VC that has a UIScrollView with many uiviews, labels, images etc... works great.
So what changed from that project to this project? The version of Xcode that I started the project out in. The project from last summer did NOT have the AutoLayout enabled as an option. This new one did ( Xcode 4.6 ).
If I turn off "Use AutoLayout" for the entire project it works fantastic.
In the Utilities panel, click on the File Inspector tab. In the section for "Interface Builder Document" you'll see a checkbox for "Use AutoLayout". De-select that and re-build your app. Just found this trick not more than 10 minutes ago for my own project -- works great.
Hope this works for your project... good luck!
I found the solution it might not be the perfect solution but it works.
The problem seemed to be somewhere in my first project since I could make it work in other projects,
so I copy'd my entire project to a different project and this way it worked.
This solution won't explain what is wrong but I think it will solve your problem.
Copy the elements from the new project you created to the old one and replace the affected view controller. Copy also the class.
There must be something wrong with your connections but with the info you provided is impossible to check.
If that doesn't work then it must be something in the navigation (doubt it) or in some custom code you have on the class (very difficult if all the code you have is in the viewDidLoad as you've shown).

UIViewController does not release its views

I was trying to fix a memory leak in my application and stumbled upon a very interesting thing. Now i'm not sure if there's a bug that i made somewhere or simply misuse of the technology so i'll try to get things clear with your help. Here's what happens:
i create a custom uiviewcontroller that loads its view from nib file
i release the controller
controller's dealloc method gets called where i release a custom view that i've specifically added to the view hierarchy as an outlet (i made a retainable property out of it). It has a dealloc method with a call to nslog.
the main view in the nib file (connected to controller's view outlet) is also a subclass of a uiview which also has a call to nslog in its dealloc
The problem is - even though the uiviewcontroller's dealloc is getting called, neither the main view nor the child (the one with outlet) gets released (their NSLogs don't fire).
Is it normal that this happens? Maybe iOS doesn't release the views right away? Or should i start looking for bugs in the code? If so - what could be the most probable causes?
Thanks for reading
The problem with late night debugging is that you don't consider even the simplest angles. Since i like to know how things work and do everything from scratch, i've created my own system for switching view controllers. The problem was that even though i used to deallocate the view controller when needed, i forgot to remove it from superview, thus having one more retain too many. Now there's a leak somewhere else, but i'm sure i'll solve it myself. Thanks for your comments.

NSCFString or UIViewController?

I am using UIViewController (a subclass of course) with a text field which sends an action when the contents changed (to the contentsChanged: selector of the ViewController). It is done by sending contentsChanged: to file's owner in IB.
But when I test it, it says : "-[NSCFString contentsChanged:] : unrecognised selector sent to instance " and the instance pointer in hex.
I am guessing that for some reason the view controller gets moved to another pointer and a string gets allocated there, but I cannot figure why.
Any ideas ?
Sounds like a classic case. Read up on NSZombieEnabled for how to track this sort of problem down.
I have the exact same problem with a subclass of UIViewController and this piece of innocuous code:
- (void)viewDidLoad
{
NSLog(#"%# %s %#", [self class], _cmd, answerButton);
[self.answerButton addTarget:self
action:#selector(getAnswerToQuestion:)
forControlEvents:UIControlEventTouchUpInside];
}
Yes, answerButton is connected (it's an IBOutlet), yes, - (IBAction)getAnswerToQuestion:(id)sender; is a proper method, but no joy. When I commented out the viewDidLoad and made the connection in IB, it showed in the crash report that the failure happens on [UIControl sendAction:to:forEvent:] resulting in
objc_msgSend() selector name: performSelector:withObject:withObject:
I can't prove it, but I suspect there's a bug somewhere in the UIKit that translates the bindings and addTarget to a call to performSelector. I'm planning to upgrade to iOS 4.01 first to see if that won't solve the problem.
UPDATE:
I'm not sure anymore that my problem really is similar to Alexandre Cassagne's but in the interest of sharing information I will not delete it just yet. I solved my problem, as so often, when I started to make an example project in order to file a bug report. Yes, clicking made answerButton call getAnswerToQuestion: like a good little object and all was fine.
The difference between the subclassed UIViewController of the example project and that of my real project was that the first also functioned as the xib's File's Owner while the second was just one of several view controller. When I moved getAnswerToQuestion: to the File's Owner in my real project, clicking answerButton worked as expected. So, my hunch that the problem lay somewhere in the translation from binding to performSelector wasn't that far off: the problem lies in the Responder Chain. I would think that establishing the Action-Target link either programmatically or in IB would bypass the Responder Chain, but apparently not.
The problem now, of course, is that Alexandre states in his question that his contentsChanged: method already is part of the File's Owner, which makes my answer irrelevant to the question.
without looking at the code, it looks like you are calling contentsChanged: on the text field's text, instead of the UIViewController subclass.
you should consider using the UITextFieldDelegate protocol to get called back when the text of a UITextField changes. I have not looked, but this is the thing I would do off the top of my head.