How to remove weak reference from iOS project - AFUrlConnectionOperation.m? - objective-c

I am using AFNetworking with AFDownloadRequestOperation to do downloads in my iPhone app.
I get an error when building:
Compile AFUrlConnectionOperation.m
The current deployment target does not support automated __weak references
In my code in the file AFUrlConnectionOperation.m this is where i can see the error:
- (void)setCompletionBlock:(void (^)(void))block {
[self.lock lock];
if (!block) {
[super setCompletionBlock:nil];
} else {
__weak __typeof(&*self)weakSelf = self;
[super setCompletionBlock:^ {
__strong __typeof(&*weakSelf)strongSelf = weakSelf;
block();
[strongSelf setCompletionBlock:nil];
}];
}
[self.lock unlock];
}
How can I resolve it and could you please explain me the logic behind this?
Thanks

Related

EXC_BAD_ACCESS on method in class

I have the following methods in my SQLiteManager.m implementation. Warning: old legacy code alert
- (void)dealloc {
[super dealloc];
if (db != nil) {
[self closeDatabase];
}
[databaseName release];
}
- (NSError *) closeDatabase
{
NSError *error = nil;
if (db != nil) {
// Set and log error somewhere, not relevant here
}
db = nil;
}
return error;
}
When I run my app in debug mode on iOS 10 iPad, it runs fine. When I run my app in release mode on iOS 10 iPad (with development certificate and provisioning profile), the app crashes on the line [self closeDatabase];, returning a EXC_BAD_ACCESS. I see in my console that self still is an SQLiteManager object. How is it possible that a reference to a method in your own class can give rise to a bad access error, and only in release mode?
PS: When I run with NSZombieEnabled = YES, the app runs fine.
I found my answer. I had to place the call [super dealloc]; to the end of the overridden dealloc method.
- (void)dealloc
{
if (db != nil) {
[self closeDatabase];
}
[databaseName release];
[super dealloc];
}

Missing context for method declaration in IOS 8 for MWPhotoBrowser

I am having problems with MWPhoto library after moving to IOS 8 and XCODE 6. The error occurs in PSTCollectionView class. I am wondering why it returns me Missing context for method declaration error. Below you can find the code and image for error.
#import <objc/runtime.h>
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
if (!signature) {
NSString *selString = NSStringFromSelector(selector);
if ([selString hasPrefix:#"_"]) {
SEL cleanedSelector = NSSelectorFromString([selString substringFromIndex:1]);
signature = [super methodSignatureForSelector:cleanedSelector];
}
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
NSString *selString = NSStringFromSelector([invocation selector]);
if ([selString hasPrefix:#"_"]) {
SEL cleanedSelector = NSSelectorFromString([selString substringFromIndex:1]);
if ([self respondsToSelector:cleanedSelector]) {
invocation.selector = cleanedSelector;
[invocation invokeWithTarget:self];
}
}else {
[super forwardInvocation:invocation];
}
}
#end
i update PSTCollectionView from github and now works fine
https://github.com/steipete/PSTCollectionView

xcode 5 dealloc void ARC Error

I get an error (ARC forbids explicit message send of 'dealloc'), if I write:
- (void)dealloc {
self.slider = nil;
self.tabBar = nil;
[super dealloc];
}
Hope anyone can help me.
Thanks a lot for answering.
Remove [super dealloc], it's automatic under ARC.
Should be just:
- (void)dealloc
{
self.slider = nil;
self.tabBar = nil;
}

Upgrade an existing singleton to ARC

In Objective-C, how to write a singleton with ARC? In ARC, it is not allowed to overwrite the release, autorelease, retain, retainCount methods, how to avoid a object to be released? I know without ARC, a classic singleton would like below:
#interface SingletonObject
+ (SingletonObject*)sharedObject;
#end
SingletonObject *sharedObj;
#implementation SingletonObject
+ (id)allocWithZone:(NSZone *)zone
{
if (sharedObj == nil) {
//So the code [[SingletonObject alloc] init] is equal with [SingletonObject sharedObject]
sharedObj = [super allocWithZone:zone];
}
return sharedObj;
}
+ (void)initialize
{
if (self == [SingletonObject class]) {
sharedObj = [[SingletonObject alloc] init];
}
}
+ (SingletonObject*)sharedObject
{
return sharedObj;
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}
- (oneway void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
- (id)init {
self = [super init];
if (self) {
//...
}
return self;
}
#end
Is it safe to just remove the retain, retainCount, release, autorelease methods? Thanks!
You only need one method to implement a class that supports the singleton pattern:
+ (instancetype)sharedInstance
{
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
Copy/paste that into any class and it'll have a shared instance. Any code beyond that is just added complexity that really isn't necessary. I'll sometimes add:
- (void)dealloc
{
*(char*)0x42 = 'b';
// no super, ARC all the way
}
That'll cause a very specific crash if my shared instance is ever deallocated due to a bug. (Yes, hex 0x42 is not 42, but it leaves a nice 0x000000042 in a register in the crash log, making it immediately identifiable what happened.)
Yes.
-sharedObject should be +sharedObject, though (class method). Just make sure you always use that.

AVCaptureSession "startRunning" crash

So here's my problem: I've created a custom AVCaptureSession that takes pictures. I'm not sure why, but the third time you call startRunning, it crashes. I implemented didReceiveMemoryWarning, and it wasn't called before it crashed. I also ran instruments on it and there were no memory leaks associated with the AVCaptureSession. There were also no logs via console in XCode.
So my question is... is it a memory problem even though the didReceiveMemoryWarning wasn't called? Here's some of my code.
viewWillDisappear (ViewController)
- (void)viewWillDisappear:(BOOL)animated {
if (cameraFlashButton) { [cameraFlashButton release]; }
if (switchCamera) { [switchCamera release]; }
if (cameraBadgeBack) { [cameraBadgeBack release]; }
if (cameraBadgeNumber) { [cameraBadgeNumber release]; }
if (cameraUseButton) { [cameraUseButton release]; }
if (cameraOverlayView) { [cameraOverlayView release]; }
if (blackOverlay) { [blackOverlay release]; }
if (loadingIndicator) { [loadingIndicator release]; }
if (cameraPickButton) { [cameraPickButton release]; }
if (whiteOverlay) { [whiteOverlay release]; }
if (imageOverlay) { [imageOverlay release]; }
if (captureManager) { [captureManager release], captureManager = nil; }
if (theCaptureSession) { [theCaptureSession release], theCaptureSession = nil; }
[super viewWillDisappear:YES];
}
dealloc (CaptureSessionManager)
- (void)dealloc {
if ([self captureSession]) { [[self captureSession] stopRunning]; }
if (previewLayer) { [previewLayer release], previewLayer = nil; }
if (captureSession) { [captureSession release], captureSession = nil; }
if (stillImageOutput) { [stillImageOutput release], stillImageOutput = nil; }
if (stillImage) { [stillImage release], stillImage = nil; }
[super dealloc];
}
Ideas? If you need to see anything else, just ask! Thanks in advance.
If didReceiveMemoryWarning was properly implemented and it was never called, your problem is likely not due to running out of memory. There are many other ways you can get a crash when you start your capture session running. You'd need to post more of your implementation along with details of the crash to help debug this.
However, the code you posted has a number of inefficiencies and style problems. Note in Objective-C messages to nil are perfectly fine. So in your viewWillDisappear and dealloc methods you can and should remove every if test readability. For example, instead of:
if (cameraFlashButton) { [cameraFlashButton release]; }
just use:
[cameraFlashButton release];
If you are using properly synthesized accessors it is also much better to replace lines like
if (previewLayer) { [previewLayer release], previewLayer = nil; }
with simply
self.previewLayer = nil;