IOS What executes before main()? - ios

objc[1655]: Object 0x2314e0 of class __NSCFString autoreleased with no
pool in place - just leaking - break on objc_autoreleaseNoPool() to
debug
objc[1655]: Object 0x2315e0 of class NSPathStore2 autoreleased with
no pool in place - just leaking - break on objc_autoreleaseNoPool() to
debug
objc[1655]: Object 0x2316b0 of class __NSCFData autoreleased with no
pool in place - just leaking - break on objc_autoreleaseNoPool() to
debug
File:MultiFormatReader.mm Method:+[MultiFormatReader load] --
objc[1655]: Object 0x2317e0 of class __NSCFString autoreleased with
no pool in place - just leaking - break on objc_autoreleaseNoPool()
to debug
objc[1655]: Object 0x231800 of class __NSCFData autoreleased with no
pool in place - just leaking
- break on objc_autoreleaseNoPool() to debug
..++++++++
File:main.mm Method:main -- mark..
File:BarcodesAppDelegate.m Method:-[BarcodesAppDelegate
application:didFinishLaunchingWithOptions:] -
File:BarcodesAppDelegate.m Method:-[BarcodesAppDelegate
application:didFinishLaunchingWithOptions:] -
This is the relative code:
+ (void)load {
MPLog(#" ..++++++++");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[FormatReader registerFormatReader:[[[self alloc] init] autorelease]];
[pool drain];
}
int main(int argc, char *argv[]) {
MPLog(#"mark..");
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
This my debug message,I used zxing framework in my APP,But I found before main() method execute, some other code executed already. Why? generally speaking, what executes before main()?
What does this program " objc[1655]: Object 0x2314e0 of class __NSCFString autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug" mean?

Move the 2 MPLogs below NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];?
+ (void)load {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MPLog(#" ..++++++++");
[FormatReader registerFormatReader:[[[self alloc] init] autorelease]];
[pool drain];
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MPLog(#"mark..");
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}

Set a symbolic breakpoint at objc_autoreleaseNoPool to halt execution when this error is encountered. Then you can determine where you need to place your autorelease pools (explicitly).
generally speaking, what excutes before main()?
from +[NSObject load] docs:
Invoked whenever a class or category is added to the Objective-C
runtime; implement this method to perform class-specific behavior upon
loading.
+ (void)load
Discussion The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.
The order of initialization is as follows:
All initializers in any framework you link to.
All +load methods in your image.
All C++ static initializers and C/C++ __attribute__(constructor) functions in your image.
All initializers in frameworks that link to you.
In addition:
A class’s +load method is called after all of its superclasses’ +load methods.
A category +load method is called after the class’s own +load method.
In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.

I'm seeing this same problem. If I put a breakpoint at objc_autoreleaseNoPool, on the main thread, I get the following useless stack trace:
0 0x332097b8 in objc_autoreleaseNoPool ()
1 0x331fe7b8 in (anonymous namespace)::AutoreleasePoolPage::autoreleaseSlow(objc_object*) ()
2 0x332098de in _ZL22_objc_rootAutorelease2P11objc_object ()
3 0x331fcdb6 in _objc_rootAutorelease ()
It seems to be hitting the autorelease error before anything in the application has actually started. Could this be the result of some load methods in one of the frameworks I have included in the project?

Related

iOS objective-C, same memory address after releasing an object

I'm learning objective-c manual memory management. I have the following example:
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSObject *o = [[NSObject alloc] init];
NSLog(#"%#", o);
[o release];
NSLog(#"%#", o);
}
return 0;
}
The output for this code is:
2015-07-31 16:18:34.694 test[2378:96008] <NSObject: 0x10020d110>
2015-07-31 16:18:34.696 test[2378:96008] <NSObject: 0x10020d110>
How is it possible, that after calling 'release' object is still present in memory (has the same address as before calling 'release') and NSLog does NOT give me EXC_BAD_ACCESS exception ?
That's because o still points to 0x10020d110, even after you've released the object.
Releasing an object does not change references to that object to nil, but they are invalid and if you attempted to send a message via the reference, undefined behaviour would ensue.
In your case, however it does not crash, as both NSLog() calls are calling [o description].

The autoreleasepool with ARC in iOS

In iOS, when the autoreleasepool create and be destroyed?
When a touch event created, the runloop create an autoreleasepool.
When the touch event ends, the autoreleasepool will be destroyed.
Is my understand of the autoreleasepool right?
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
how can I understand the autoreleasepool in main method?
In xcode 4 we have to create pool and we have release it by using this syntax with NSAutorealeasePool:
{
NSAutorealeasePool *pool=[[NSAutorealeasePool alloc] init];
[pool drain];
}
When ever use like this in Xcode 4 memory is initialized for your pool and control enter into your pool deallocate your pool objects by using drain ..if u use drain means u permanently destroy ypur pool objects..if you use release means its not completely destroyed..
In xcode 5 we are using #autoreleasepool syntax like this:
#autoreleasepool
{
}
Here we don't need to create pool and release the objects..every thing done by compiler..once control enters automatically create the pool and release the pool objects means which we want to deallocate
The NSAutoreleasePool class is used to support Cocoa’s reference-counted memory management system. An autorelease pool stores objects that are sent a release message when the pool itself is drained.
IMPORTANT
If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use #autoreleasepool blocks. For example, in place of:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Code benefitting from a local autorelease pool.
[pool release];
you would write:
#autoreleasepool {
// Code benefitting from a local autorelease pool.
}
#autoreleasepool blocks are more efficient than using an instance of NSAutoreleasePool directly; you can also use them even if you do not use ARC.
Few things to keep in mind -
Case 1)
#autoreleasepool
{
MyObject *obj = [[MyObject alloc] init];
//No need to do anything once the obj variable is out of scope there are no strong pointers so the memory will free
}
case 2)
MyObject *obj //strong pointer from elsewhere in scope
#autoreleasepool
{
obj = [[MyObject alloc] init];
//Not freed still has a strong pointer
}

Why can [SomeClass class] return null in main.m

I am trying to make an iOS app without using xibs or storyboards. So my main.m
looks like this:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
[pool release];
return retVal;
}
If I check value of [AppDelegate class] it is (null).
So the question is, why?
Also if i check [NSString class] or other class from standard library it performs normally.
I thought that file is not in Compile sources list, but it's there. This problem only occurs in one project.
Check if you have initialized assigned some controller to your window's root controller in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
Check out this answer.

How does main.m works in iOS?

int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
will UIApplicationMain() call will return only after completion of our application execution?
because i have next line [pool release]
how can i pass the command line argument in main()?
What are the possible int value that UIApplicationMain() will return and their state?
will UIApplicationMain() call will return only after completion of our application execution? because i have next line [pool release]
UIApplicationMain() will never return. The [pool release] and return are just for aesthetic balance.
how can i pass the command line argument in main()?
The command line arguments are already there in argv. You can do what you like with them prior to UIApplicationMain(). If you mean "how do I access them outside of main()," see NSProcessInfo. This is generally not very useful in iOS.
What are the possible int value that UIApplicationMain() will return and their state?
There are none. See the UIKit Function Reference.

Why does Xcode 4.2 use #autoreleasepool in main.m instead of NSAutoreleasePool?

I've noticed that there is a different way in Xcode 4.2 to start the main function:
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([PlistAppDelegate class]));
}
}
and
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Does anybody know the difference between those two?
The first one is using ARC, which is implemented in iOS5 and above to handle memory management for you.
On the second one, you're managing your own memory and creating an autorelease pool to handle every autorelease that happens inside your main function.
So after reading a bit on what's new on Obj-C with iOS5 it appears that the:
#autoreleasepool {
//some code
}
works the same as
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// some code
[pool release];
with the difference that the last one would throw an error on ARC.
EDIT:
The first one is using ARC or not.

Resources