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.
Related
If I remember correctly, back in the days Xcode used to generate the main function something like this (at least for iOS applications):
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]);
}
}
I.e. UIApplicationMain function was wrapped inside of #autoreleasepool. However I noticed that currently Xcode no longer does that:
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
#autoreleasepool {
// Setup code that might create autoreleased objects goes here.
appDelegateClassName = NSStringFromClass([AppDelegate class]);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
So, UIApplicationMain is outside of the autorelease pool by default nowadays. Is there any official changelog/paper that explains why the latter implementation is now preferred over the former one?
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].
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.
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?
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.