I read on the web that when I create an object with alloc and init I have to release it (even a NSString), so:
Why if I create a NSString this way:
NSString *prova = [[NSString alloc] init];
[prova release];
I get these errors:
'release' is unavailable: not available in automatic reference counting mode
and
ARC forbids explicit message send of 'release'
on the [prova release] message? I get no error when I try do this:
NSString *prova = [[NSString alloc] init];
NSLog(#"Contenuto 0 di prova: %#", prova);
prova = #"prima prova stringa";
NSLog(#"Contenuto 1 di prova: %#", prova);
prova = #"ma cosè questo fantomatico errore";
NSLog(#"Contenuto 2 di prova: %#", prova);
That's a best practice previous to iOS 5, or in iOS 5 if ARC mode is disabled. Now iOS 5 uses the new Apple's LLVM compiler, which introduces this ARC feature.
So if ARC is enabled (and it is by default), you do not need to use, in general, the release method anymore. You can find more details in documentation.
If you still want to develop the old way, you can add the flag -fno-objc-arc in "Build phases" section of a Xcode project
You are using Apple's new ARC (automatic reference counting). ARC is a new compiler function which adds retain, release and autorelease on compile time automatically.
Have a look at the iOS 5 Release Nodes for more information about ARC: http://developer.apple.com/technologies/ios5/
Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.
in ios 5 no need to release objects it automatically release your object.
or another way disable "Objective-C Automatic Reference Counting" ARC from your xcode
ARC is the new feature in IOS5 which means Automatic reference counting... You dont need to look after release and all as you using arc in your app .. You can de-select when you start a new project in the checkbox.
Related
My code suddenly can't be compiled in Xcode 6.1 (I'm sure it's working in Xcode 6 GM and beta version). It shows the error message:
'NSInvocationOperation' is unavailable
My code is:
let operation = NSInvocationOperation(target:self, selector:"backgroundRun:", object:self)
Can anybody help? Thanks.
As of Xcode 6.1, NSInvocation is disabled in Swift, therefore, NSInvocationOperation is disabled too. See this thread in Developer Forum
Because it's not type-safe or ARC-safe. Even in Objective-C it's very very easy to shoot yourself in the foot trying to use it, especially under ARC. Use closures/blocks instead.
You have to use NSBlockOperation in Swift.
or addOperationWithBlock to NSOperationQueue
queue.addOperationWithBlock { [weak self] in
self?.backgroundRun(self)
return
}
I am trying to use Apple's 'BTLE Transfer' sample project to understand CoreBluetooth programming. The app runs fine if I use an iOS 6 device as the Central, but if I run the same app with the iOS 7 device as the Central it doesn't work. The peripheral stops sending after two packets, and the central doesn't receive either one of them.
The only clue is this warning that I get only when running on iOS 7:
CoreBluetooth[WARNING] <CBCentralManager: 0x15654540> is disabling duplicate filtering, but is using the default queue (main thread) for delegate events
Can anyone tell me what needs to change to make this app compatible with iOS 7?
EDIT: When both devices are iOS7 there are no issues. This only breaks when an iOS7 central is talking to a iOS6 peripheral.
Okay I just ran it on an iOS 7 central to iOS 6 peripheral. If you want to make that warning about disabling duplicate filtering go away, just run it on a different thread. Do something like this:
dispatch_queue_t centralQueue = dispatch_queue_create("com.yo.mycentral", DISPATCH_QUEUE_SERIAL);// or however you want to create your dispatch_queue_t
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue];
Now it will allow you to scan with duplicates enabled. However, you must call the textView setter on the main thread to be able to set the text without crashing:
dispatch_async(dispatch_get_main_queue(), ^{
[self.textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];
});
Btw you probably also want to adopt the new iOS 7 delegate initialization:
_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:centralQueue options:nil];//set the restoration options if you want
(Just check the iOS version and call the appropriate initialization method)
InscanForPeripheralsWithServices:options:,if you set CBCentralManagerScanOptionAllowDuplicatesKey:#YES then change it to
CBCentralManagerScanOptionAllowDuplicatesKey:#NOthat means scan should run without duplicate filtering.
For me,it works on iOS7 also.
I have an ARC lite enabled app (ARC enabled without zeroing weak reference, Apple LLVM 4.0). Deployment target set to iOS 4.0. It's been tested on iOS 4.0 to 5.1.1. It always works fine when I use debug build and install through Xcode, but when I create an adhoc build with release configuration, it always crash during launch on 2nd gen iPod with iOS 4.2.1, but works fine on other devices (even 3rd gen iPod with iOS 4.3).
The crash log says
symbol not found: _objc_retainAutoreleasedReturnValue
and it crashes when method main is called (then it's dyld_stub_binder, dyld::fastBindLazySymbol)
I did some search and this crash normally happens when an arc enabled library is included in a project without arc. But in my case arc is enabled for the whole project.
Any clues?
Thanks very much.
[EDIT] Update: I can use NSLog to debug the adhoc build.
The main.m is like:
int main(int argc, char *argv[]) {
NSLog(#"ok before entering autorelease pool");
#autoreleasepool {
NSLog(#"ok after entering autorelease pool");
return UIApplicationMain(argc, argv, nil, NSStringFromClass([XXX class]));
}
}
NSLog(#"ok after entering autorelease pool") gets called successfully, but application:didFinishLaunchingWithOptions: is never entered.
It seems that it's related to the command line build tool. I used to use xcodebuild command to archive the adhoc build:
xcodebuild -scheme myscheme clean build archive
And got this crash.
Then I tried archiving from Xcode, the adhoc build actually works fine on all devices. So weird (keep in mind that the command line built adhoc worked for devices newer than iPod 2nd gen).
For now I'll just use the Archive button in Xcode. But I posted this question in Apple dev forum:
https://devforums.apple.com/message/711334
My answer: How i come out from the same problem.
Steps:
1.First make the all the changes in the code sign column and make sure that all the setting are work same as you want in the project tab and target tab as well.
2.Just making all the setting in the first step like selecvting correct provsioning and specify accurate bundle indetifier then now make the xcode quit .
3.After 2 step just restart you computer and then make build for the profile that you selected in first step.
Now its working fine ,hope so this answer may be help you as well.Anyway its my solution of the above problem.....:)
I have added ALAssetsLibrary framework to my project and for some reason XCode doesnt detect it.
I have no code completion, and XCode gives a warning and then crashes when I use the following code
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error){
NSLog(#"A problem occured");
}];
It doesnt detect what ALAssetsLibrary is and the warning which leads to a crash is
warning: 'ALAssetsLibrary' may not respond to '-enumerateGroupWithTypes:usingBlock:failureBlock:'
Why isnt XCode detecting the framework?
I have added it to my project as well as imported it.
I would really appreciate the help with this, I dont understand what I did wrong. Thanks
Choose "Device", not "Simulator".
First, you're importing AssetsLibrary.h, are you sure this refers to the class ALAssetsLibrary and ALAssetsGroupAlbum?
Second, I'm not sure it failing to find it is the problem, I imagine there would be a different error in that case, such as, this class is not defined, first use in function.
It's saying unrecognized selector, which means there might be a problem with your pointers.
Have you got your SDK set to 3.x? Blocks were only introduced in iOS 4.0, along with all the methods relating to them.
I am developing a quiz app. I take questions from a xml file, parse it and display random questions. These are stored in NSdictionary and NSMutableArray. Also the app plays background music and sound for clicking of buttons(AVAudioPlayer). and vibration( AudioServicesPlaySystemSound(kSystemSoundID_Vibrate))
In one particular function if i try to release the temp variables that I use(I.E NSDictionary and NSMutableArray) the app crashes wen i reach that function for the second time. Hence if i don release these, it works fine but eventually crashes with a "EXC_BAD_ACCESS" ERROR. It does not point out to any line or function.
When i used the tool "LEAKS", it showed i was having around 7000 leaks. I don understand how to use that tool but I am sure that i am not creating so many variables, jus a few and even those I release.
And just once i got the ERROR " data formatters temporarily unavailable".
Any Idea what i am doing wrong?? F1 :)
PS: my code is all simple stuff, plus i donno what the problem is hence i donno what code to post here?
Also i would like to know if i create a NSString* in a function for temp use should i release it at the end of the function?(i do release it)
EDIT:
-(void) loadQuestion
{
strCorrectAnswer = #"";
int intQuestionNo;
NSString *strQuestionNo = [[NSString alloc] init];
// get random question out the xml file
NSDictionary *dctQue = [dctQuestions objectForKey:strQuestionNo];
// blah blah
// jumble the answers to be displaed
NSMutableArray *answerJumble = [[NSMutableArray alloc] init];
NSMutableArray *answers =[NSMutableArray arrayWithObjects:[dctQue objectForKey:#"WrongAnswer1"],[dctQue objectForKey:#"WrongAnswer2"],[dctQue objectForKey:#"WrongAnswer3"],[dctQue objectForKey:#"CorrectAnswer"],nil];
// blah blah
/*
[strQuestionNo release];
[answers release];
[answerJumble release]; */
}
You should read something about memory management in Cocoa. See the Mac Developer Center or the tutorial at Cocoa Dev Central. Memory management on iPhone is not hard, it’s a pity to code by trial and error.
well after a bit of digging, the problem was wen the sound file had to be replayed. If i press a button and before the sound file finishes playing if i press again, the sound file was being played just once. Resulting in a memory leak of 3000.
IF i did this twice the app wud crash after a leak of 6425. Hence the ERROR-"data formatters currently not available".(i guess)