iOS swift CTFontManagerRegisterGraphicsFont vs CTFontManagerRegisterFontsForURL - ios

I am using CTFontManagerRegisterGraphicsFont to register CGFont loaded at runtime from various sources (files, memory, ..), but some fonts raises an Exception with the following message:
Exception: "*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]"
an alternative to CTFontManagerRegisterGraphicsFont is CTFontManagerRegisterFontsForURL:
using this function I did not have any exception raised.
My Questions:
What is the cause of this behavior difference
How to catch & discard the exception raised by CTFontManagerRegisterGraphicsFont
Is there any thing to be done before calling CTFontManagerRegisterGraphicsFont to prevent the exception.
What .persistent scope mean for 2nd parameter in CTFontManagerRegisterFontsForURL.

Related

throw Exception('oops!') vs throw 'oops!'

I notices some code that made me think the Exception function call was optional? E.g., do these two lines perform the same function?
throw Exception('oops!');
throw 'oops!'
No.
The former, throw Exception('oops!');, creates a new Exception object using the Exception constructor, then throws that object.
It can be caught by try { ... } on Exception catch (e) { ... }.
The latter, throw 'oops!'; throws the string object.
It can be caught by try { ... } on String catch (e) { ... }.
Generally, you shouldn't be doing either.
If someone made an error, something that would have been nice to catch at compile-time and reject the program, but which happens to not be that easy to detect, throw an Error (preferably some suitable subclass of Error).
Errors are not intended to be caught, but to make the program fail visibly. Some frameworks do catch errors and log them instead. They're typically able to restart the code which failed and carry on, without needing to understand why.
If your code hit some exceptional situation which the caller should be made aware of (and which prevents just continuing), throw a specific subclass of Exception, one which contains the information the caller needs to programmatically handle that situation. Document that the code throws this particular exception. It's really a different kind of return value more than it's an error report. Exceptions are intended to be caught and handled. Not handling an exception is, itself, an error (which is why it's OK for an uncaught exception to also make the program fail visibly).
If you're debugging, by all means throw "WAT!"; all you want. Just remove it before you release the code.

How can I save the fatalError message to the iOS crash log?

I have an iOS application written in Swift 2 in Xcode 8.2.1, that's built for iOS 10.2.
I've had a number of crash reports from TestFlight and despite symbolication, none of the crash logs show any program state besides the stack-traces (no argument values, no locals, no heap objects, etc).
...but inside those functions I can see code which is likely to fail (e.g. a forced unwrap) but the crash log isn't telling me where or why it's failing.
When debugging in Xcode, I can use fatalError(message: String) where I can put my own message like "functionFoo returned nil" or "variable bar == \"" + bar + "\"", except when deployed using TestFlight or the App Store the fatalError will be hit and the program terminates, but the message value is not saved to the crash log, making it pointless.
In other environments, like C#/.NET and Java I can simply throw new SomeExceptionType("my message") and all information is available in whatever global catch(Exception) handler I have.
How can I achieve the same goal in iOS / Swift?
Swift does support error handling. You can create your own error type by confirming to Error protocol or use existing error types and then throw an error by invoking throw error.
But Swift forces you add error handling to any code that can throw an error. There are multiple way you can handle error in swift.
Apply throws keyword to your function, this indicates that the function can throw an error when invoked and the error should be handled by the caller.
func canThrowErrors() throws -> String
When invoking methods with throws keyword you have to add try keyword at the beginning of the invocation. All these try invocations should be handled either by applying throws to method to just propagate the errors or wrapping inside a do-catch block:
do {
try canThrowErrors()
try canThrowOtherErrors()
} catch is SpecificError {
// handling only specific error type
} catch let error as SpecificError {
// catches only specific error for type
} catch {
// catches all errors
}
Additionally you can use try? and try! for throwing function invocation to disable error propagation and retrieve optional result that returns nil in case of error and runtime assertions respectively.
By forcing you to handle all the errors at compile time swift avoids any undefined runtime behavior and debugging nightmare.
I would suggest to use fatalError or any other runtime assertion only if scenarios when there is no way to recover from a state without crashing the app. Unfortunately, there is no way to handle errors from fatalError as its use is only reserved for such scenarios only. Also, in your crashlog you will only get the line number that caused the crash to get additional info for the cause of crash I would suggest to use custom logging or analytics.

Exception with insertObject:atIndex: on iOS6

I'm getting the following Exception on iOS6 (on an App with CoreData):
"2012-10-15 10:21:28.952 MyApp[68650:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
* First throw call stack:
(0x28e6012 0x2659e7e 0x2899b6a 0x2899a20 0x1646941 0x1642c67 0x164f846 0x164f908 0x6c540 0x2057e83 0x28a5376 0x28a4e06 0x288ca82 0x288bf44 0x288be1b 0x33967e3 0x3396668 0x15a165c 0x13a22 0x2845)
libc++abi.dylib: terminate called throwing an exception"
This doesn't happen on iOS5, so something happens on iOS6 what I don't understand.
I set a Breakpoint on every point where I call insertObject:atIndex: but these are not called - it have to be something in this libc++abi.dylib which gets called and crashes.
Does anyone know what could be wrong?
thank you
This is probably because either iOS5 did not throw an exception for this error (and should have, but now iOS6 throws one which is better than having erratic behavior later), or because you have some different behavior in iOS6 which makes your object nil whereas it was not in iOS5.
Whatever the reason, you can add a Symbolic Breakpoint on the insertObject:atIndex: symbol, so that it will break every time this method is called, wherever it is in your application (in your own code or not).
Go to the "Breakpoints Navigator" view (Cmd-6 shortcut)
Click on the "+" button to add a symbolic breakpoint
Set the symbolic breakpoint to break when it hits the symbol [NSArray insertObject:atIndex:]
Thus you can see when this is called with a nil value for the first parameter and fix your problem where it occurs.
You can also instead add an Exception Breakpoint to break when an exception is thrown, thus knowing when in the code your exception occurs. This is another way to let you know which part of the code (your own or another) generate the exception.
Once the breakpoint has been hit and the program stops before the exception occurs, you can check in the call stack what part of your own code led to trigger this exception at the end.
The reason for the crash is that the object you are trying to insert is nil. This means it is not properly instantiated. This in turn means something has gone awry before you reached that exception.
Could you post the code that alloced and initialized the object you were trying to insert?
In order to find the relevant line of code, please try the following: Go to the "Exception" tab in your Xcode project:
Then click the "+" button (at the bottom of the page) and select "Add Exception Breakppoint ...". Leave all settings to their defaults and click "Done".
If you rerun your project it should now stop at the relevant line of code before the exception is thrown. Then you can move up the call-stack and identify from where in your code you called the library function that is responsible for this behavior. Then try to see if all objects are correctly initialized at this point.

File Open and Exception

does file related functions throw an exception when it fails OR just return false.
Coz even if I use it in try-catch, there is no point in catching an exception if fopen/fwrite does not throw it at all…
Return Values
Returns a file pointer resource on success, or FALSE on error.
Errors/Exceptions
If the open fails, an error of level E_WARNING is generated. You may use # to suppress this warning.
Check documentation. you can throw custom exception if you want to.

Find the root cause of a first chance exception [duplicate]

I have a project that runs perfect under windows xp.
Now I have tried to run it under Windows 7 and got there a lot of exceptions under Immediate window.
A first chance exception of type 'System.ArgumentNullException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in LP_Wizard.exe
A first chance exception of type 'System.ArgumentException' occurred in LP_Wizard.exe
A first chance exception of type 'System.NullReferenceException' occurred in LP_Wizard.exe
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.ArgumentNullException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in LP_Wizard.exe
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.ArgumentNullException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in LP_Wizard.exe
A first chance exception of type 'System.ArgumentNullException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in LP_Wizard.exe
Any idea what wrong with that Microsoft.VisualBasic.dll in windows 7 and how i correct that problem ?
Thanks a lot for help .
If you want to pinpoint where the exceptions are occurring, you can select the Debug->Exceptions menu item, and in the dialog that appears, check the first checkbox for "Common Language Runtime Exceptions". This will make the debugger break as soon as an exception occurs instead of only breaking on unhandled exceptions.
This is also one reason why it is generally a bad idea to catch generic exceptions unless you are clearly logging the information caught.
What is happening is the debugger can "see" exceptions as soon as they are raised (hence the "first chance") before any catch block is hit. Any exception which is not handled by a catch block is considered a "second chance" exception and will break normally.
If these exceptions aren't stopping the running of your application because they are unhandled then you are probably OK. Most of the time the exception is handled by code and this isn't a problem. The output is simply Visual Studio letting you know the exceptions were raised.
See the "Avoiding first chance exception messages when the exception is safely handled" question for some methods to reduce this if there are too many to ignore.
Are your in the debugger? Are these exceptions your program is handling? If so you need to find a setting that tells VB to supress warning you of handled exceptions. Maybey this was set when installed on XP but not when you installed on W7. See if this helps:
http://www.helixoft.com/blog/archives/24

Resources