2

I'm experiencing an access problem in my Cordova plugin: my NSFileHandle "loses" context between Cordova calls, and I end up with either an EXC_BAD_ACCESS, a SIGABRT or an Unrecognized Selector sent to instance error. Debugging and digging inside Obj-C's documentation has given me no lead on this, so I'd appreciate your help here very much!

Here's my code. First - the interface:

@interface MyPlugin : CDVPlugin
...
- (void) startWriting:(CDVInvokedUrlCommand*)command;
- (void) stopWriting:(CDVInvokedUrlCommand*)command;
@end  

And the implementation:

....
static NSFileHandle *logFile;


@implementation MyPlugin

- (void) startWriting:(CDVInvokedUrlCommand*)command{
    logFile = [NSFileHandle fileHandleForWritingAtPath:@"path_to_my_file"];
    NSData nsData = [@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding];
    [logFile writeData:nsData];
}

- (void) stopWriting:(CDVInvokedUrlCommand*)command{
    NSData nsData = [@"Goodbye World!" dataUsingEncoding:NSUTF8StringEncoding];
   [logFile writeData:nsData];   
}  

I call startWriting and then stopWriting using cordova.exec. The error occurs on the last line of stopWriting. There were a few times that the problem miraculously disappeared, but in most cases I get one of the aforementioned errors.
It appears that my logFile object closes the file seamlessly, but according to iOS documentation, this usually happens when the NSFileHandle object is deallocated, whereas my object is declared as static, and is not supposed to be deallocated as long as my plugin lives (plus, I see in the XCode debugger that it is still allocated).

What, in your opinion, causes my NSFileHandle object to "lose" the actual file?

  • http://stackoverflow.com/questions/15683624/exc-bad-access-sigabrt-ios-crash-log. – iPatel Jul 24 '13 at 08:43
  • Do you mean to say that the source for my problem is unknown? Because I debugged the code and kept failing on the same `[logFile writeData:nsData]` line over and over again. I'm sure that this is the problematic line, because `stopWriting` is called as the result of a button pressed by me. – Warm Chocolate Jul 24 '13 at 09:14
  • i don't know what is real problem but i just want to help you :) thanks :) – iPatel Jul 24 '13 at 09:16
  • Thank you! So do you think I can gain from using the DebugConsole offered in the link you pasted, if I already know which line is failing and that the `logFile` object is not **nil** in that line? – Warm Chocolate Jul 24 '13 at 09:29

1 Answers1

1

Imho - logFile is released once function finishes its job. You should change your code to something like

if (logFile==nil) logFile = [NSFileHandle fileHandleForWritingAtPath:@"path_to_my_file"];

or manually retain/release the logFile object.

Anton
  • 3,166
  • 1
  • 13
  • 12
  • Well, using this code snippet (according to your second suggestion) helped me solve the problem: `if (logFile==nil) logFile = [[NSFileHandle fileHandleForWritingAtPath:@"path_to_my_file"]retain];`. Thanks!!! Does this mean that **static** variables don't last as long as the encapsulating object lasts? I mean - doesn't the line `static NSFileHandle *logFile;` mean that `logFile` has a reference even after the method ends? – Warm Chocolate Jul 24 '13 at 11:40
  • Maybe you want to make it "strong" instead of "static"? ( http://stackoverflow.com/questions/11013587/differences-between-strong-and-weak-in-objective-c ) – Nathan H Jul 24 '13 at 13:36
  • Thanks! But I want to have an ivar, not a property, and as far as I understand only properties may be defined as strong. – Warm Chocolate Jul 24 '13 at 14:42