presentPopoverFromRect:inView:permittedArrowDirections:animated crashing in iOS 6.0 [duplicate] - uiimagepickercontroller

This question already has answers here:
Crash on presenting UIImagePickerController under iOS 6.0
(5 answers)
Closed 8 years ago.
I just upgraded from iOS 5.1.1 to 6.0 and the following code path is crashing with SIGTRAP when it attempts to display a popover with an imagePickerViewController as its contentVC to allow the user to select an image or video from the photoLibrary.
It was working great with 5.1.1. I've been troubleshooting this for a couple of days with no headway.
Does anyone have a handle on this? Is there an alternative method to use for iOS6.0? I can add more code if needed...
I can capture images and video with the line:
[self presentViewController:self.imagePickerViewController.imagePickerController animated:YES completion:NULL];
Here is the method in full called after the user touches a UIButton.
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType mediaType:(NSString *)mediaType
{
if (self.capturedMovies.count > 0)
[self.capturedMovies removeAllObjects];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.imagePickerViewController setupImagePicker:sourceType mediaType:mediaType];
if (sourceType == UIImagePickerControllerSourceTypeCamera) { // WORKS
[self presentViewController:self.imagePickerViewController.imagePickerController animated:YES completion:NULL];
}
else if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie] || [mediaType isEqualToString:(NSString *)kUTTypeImage]) {
if (!self.moviePickerPopoverController) {
self.moviePickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.imagePickerViewController.imagePickerController];
}
UIView *uiViewObject = self.mediaTitleTextField;
CGFloat xLocation = uiViewObject.frame.origin.x;
CGFloat yLocation = uiViewObject.frame.origin.y;
CGFloat width = uiViewObject.bounds.size.width;
CGFloat height = uiViewObject.bounds.size.height;
// CRASHES HERE!!!
[self.moviePickerPopoverController presentPopoverFromRect:CGRectMake(xLocation, yLocation, width, height)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
}
Here is what pops up in the code window:
Thread 1: signal SIGTRAP on line 3:
libsystemkernel.dylib`_kill:
0x394e7830: mov r12, #37
0x394e7834: svc #128
0x394e7838: blo 0x394e7850 ; __kill + 32 <<<<<<<<< Thread 1: signal SIGTRAP
0x394e783c: ldr r12, [pc, #4] ; __kill + 24
0x394e7840: ldr r12, [pc, r12]
0x394e7844: b 0x394e784c ; __kill + 28
0x394e7848: ldrdeq r6, r7, [r0], -r12
0x394e784c: bx r12
0x394e7850: bx lr
Here is the backtrace from the debugger:
(lldb) bt
* thread #1: tid = 0x2503, 0x394e7838 libsystem_kernel.dylib`__kill + 8, stop reason = signal SIGTRAP
frame #0: 0x394e7838 libsystem_kernel.dylib`__kill + 8
frame #1: 0x001275d4 MyAppName`TFHandleExceptions + 992
frame #2: 0x36bd357e CoreFoundation`__handleUncaughtException + 614
frame #3: 0x39313a64 libobjc.A.dylib`_objc_terminate() + 128
frame #4: 0x3363c07a libc++abi.dylib`safe_handler_caller(void (*)()) + 78
frame #5: 0x3363c114 libc++abi.dylib`std::terminate() + 20
frame #6: 0x3363d598 libc++abi.dylib`__cxa_rethrow + 88
frame #7: 0x393139d0 libobjc.A.dylib`objc_exception_rethrow + 12
frame #8: 0x36b19f20 CoreFoundation`CFRunLoopRunSpecific + 456
frame #9: 0x36b19d48 CoreFoundation`CFRunLoopRunInMode + 104
frame #10: 0x38ca82ea GraphicsServices`GSEventRunModal + 74
frame #11: 0x3701d300 UIKit`UIApplicationMain + 1120
frame #12: 0x000629b0 MyAppName`main + 96 at main.m:16

This is a duplicate of https://stackoverflow.com/a/12575058/1074338
Also, I should have touched the go button on the debugger...
which revealed the offending code:
*** Terminating app due to uncaught exception
'UIApplicationInvalidInterfaceOrientation',
reason: 'Supported orientations has no common orientation
with the application, and shouldAutorotate is returning YES'

Related

Crashed in try! realm.write

My Project Crashed in RealmUtil.swift line 16, the codes are shown below.
Is this mean that realm return nil?
I was quite confused. Hoping to get your help.
Great Thanks!
RealmUtil.swift
15 let realm = try! Realm()
16 try! realm.write {
17 write()
18 }
detail log are as below.
It's shown in firebase, and I wasn't able to debug this in debug mode.
Crashed: com.apple.main-thread
0 libsystem_kernel.dylib 0x1978b5ec4 __pthread_kill + 8
1 libsystem_pthread.dylib 0x1977d5724 pthread_kill$VARIANT$armv81 + 216
2 libsystem_c.dylib 0x197725844 abort + 100
3 Realm 0x1041bfe30 please_report_this_error_to_help_at_realm_dot_io + 10
4 Realm 0x1041c00fc realm::util::terminate_internal(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) + 264
5 Realm 0x1041c0270 realm::util::terminate(char const*, char const*, long, std::initializer_list<realm::util::Printable>&&) + 324
6 Realm 0x103eed0f4 realm::_impl::RealmCoordinator::advance_schema_cache(unsigned long long, unsigned long long) + 182
7 Realm 0x103fc0c2c realm::Realm::cache_new_schema() + 100
8 Realm 0x103fc1510 realm::Realm::begin_transaction() + 176
9 Realm 0x103f900e0 -[RLMRealm beginWriteTransaction] + 20
10 RealmSwift 0x10477004c $s10RealmSwift0A0V5write16withoutNotifying_xSaySo20RLMNotificationTokenCG_xyKXEtKlF + 136
11 Ig Followers 0x102da91c8 specialized static HomeViewModel.engagementAnalysis(media:) + 16 (RealmUtil.swift:16)
12 Ig Followers 0x102da9408 specialized static HomeViewModel.analysisLTS() + 4301886472 (<compiler-generated>:4301886472)
13 Ig Followers 0x102c84b2c closure #1 in closure #1 in HomeVC.getLTSData() + 4300688172 (<compiler-generated>:4300688172)
14 Ig Followers 0x102dab644 partial apply for closure #1 in closure #1 in static HomeViewModel.getLTSData2(pk:completeHandler:) + 4301895236 (<compiler-generated>:4301895236)
15 Ig Followers 0x102c9f454 thunk for #escaping #callee_guaranteed () -> () + 4300797012 (<compiler-generated>:4300797012)
added image about detail function about this problem.
crashed_function_image_click_to_see
The problem is how the optionals are being handled. The issue is here
let lts = HVMlts()!
as that code force unwraps the return value guaranteeing HVMlts will never be nil.
However if you look at the associated functions, it can in fact, return nil.
if results.count == 1 {
return results[0]
} else {
return nil
}
If the Realm filter
let results = realm.objects(HomeViewModel.self)...
returns two or more results, then that function would return nil. Likewise if there were no results then it would also return nil
In either case that nil would cause your code to crash.
It's best to safely unwrap optionals or provide default values using a nil coalescing operator. I see you've done that in other parts of your code but here are two more options.
if let lts = HVMlts() then {
//do something with lts as it's not nil
}
or
guard let lts = HVMlts() else { return }
//do something with lts

How can I make my arduino print "the loop is running for the ....... time"?

i want my arduino to tell me what time running it is E.g. "this is the 22nd time this loop has run." what command/s should i use?
i am currently using this code:
Serial.print("This loop has run ");
Serial.print(loopsRun);
Serial.println(" times.");
loopsRun++;
yes i have declared all variables, i just want to know if there is a way to check the last digit of any int.
22 % 10 = 2 you'd say 22'nd'
1022 % 10 = 2 you'd say 1022 'nd'
27 % 10 = 7 you'd say 7 'th'
457 % 10 = 7 you'd say 457 'th'
Am I getting the pattern right? If so then you need a switch statement and a % operator
unsigned int remainder = loopsRun % 10;
switch (remainder)
{
case 0: suffix = "th"; break;
case 1: suffix = "st"; break;
case 2: suffix = "nd"; break;
<etc>
}

Implement recursion in ASM without procedures

I'm trying to implement functions and recursion in an ASM-like simplified language that has no procedures. Only simple jumpz, jump, push, pop, add, mul type commands.
Here are the commands:
(all variables and literals are integers)
set (sets the value of an already existing variable or declares and initializes a new variable) e.g. (set x 3)
push (pushes a value onto the stack. can be a variable or an integer) e.g. (push 3) or (push x)
pop (pops the stack into a variable) e.g. (pop x)
add (adds the second argument to the first argument) e.g. (add x 1) or (add x y)
mul (same as add but for multiplication)
jump (jumps to a specific line of code) e.g. (jump 3) would jump to line 3 or (jump x) would jump to the line # equal to the value of x
jumpz (jumps to a line number if the second argument is equal to zero) e.g. (jumpz 3 x) or (jumpz z x)
The variable 'IP' is the program counter and is equal to the line number of the current line of code being executed.
In this language, functions are blocks of code at the bottom of the program that are terminated by popping a value off the stack and jumping to that value. (using the stack as a call stack) Then the functions can be called anywhere else in the program by simply pushing the instruction pointer onto the stack and then jumping to the start of the function.
This works fine for non-recursive functions.
How could this be modified to handle recursion?
I've read that implementing recursion with a stack is a matter of pushing parameters and local variables onto the stack (and in this lower level case, also the instruction pointer I think)
I wouldn't be able to do something like x = f(n). To do this I'd have some variable y (that is also used in the body of f), set y equal to n, call f which assigns its "return value" to y and then jumps control back to where f was called from, where we then set x equal to y.
(a function that squares a number whose definition starts at line 36)
1 - set y 3
2 - set returnLine IP
3 - add returnLine 2
4 - push returnLine
5 - jump 36
6 - set x y
...
36 - mul y 2
37 - pop returnLine
38 - jump returnLine
This doesn't seem to lend itself to recursion. Arguments and intermediate values would need to go on the stack and I think multiple instances on the stack of the same address would result from recursive calls which is fine.
Next code raises the number "base" to the power "exponent" recursively in "John Smith Assembly":
1 - set base 2 ;RAISE 2 TO ...
2 - set exponent 4 ;... EXPONENT 4 (2^4=16).
3 - set result 1 ;MUST BE 1 IN ORDER TO MULTIPLY.
4 - set returnLine IP ;IP = 4.
5 - add returnLine 4 ;RETURNLINE = 4+4.
6 - push returnLine ;PUSH 8.
7 - jump 36 ;CALL FUNCTION.
.
.
.
;POWER FUNCTION.
36 - jumpz 43 exponent ;FINISH IF EXPONENT IS ZERO.
37 - mul result base ;RESULT = ( RESULT * BASE ).
38 - add exponent -1 ;RECURSIVE CONTROL VARIABLE.
39 - set returnLine IP ;IP = 39.
40 - add returnLine 4 ;RETURN LINE = 39+4.
41 - push returnLine ;PUSH 43.
42 - jump 36 ;RECURSIVE CALL.
43 - pop returnLine
44 - jump returnLine
;POWER END.
In order to test it, let's run it manually :
LINE | BASE EXPONENT RESULT RETURNLINE STACK
------|---------------------------------------
1 | 2
2 | 4
3 | 1
4 | 4
5 | 8
6 | 8
7 |
36 |
37 | 2
38 | 3
39 | 39
40 | 43
41 | 43(1)
42 |
36 |
37 | 4
38 | 2
39 | 39
40 | 43
41 | 43(2)
42 |
36 |
37 | 8
38 | 1
39 | 39
40 | 43
41 | 43(3)
42 |
36 |
37 | 16
38 | 0
39 | 39
40 | 43
41 | 43(4)
42 |
36 |
43 | 43(4)
44 |
43 | 43(3)
44 |
43 | 43(2)
44 |
43 | 43(1)
44 |
43 | 8
44 |
8 |
Edit : parameter for function now on stack (didn't run it manually) :
1 - set base 2 ;RAISE 2 TO ...
2 - set exponent 4 ;... EXPONENT 4 (2^4=16).
3 - set result 1 ;MUST BE 1 IN ORDER TO MULTIPLY.
4 - set returnLine IP ;IP = 4.
5 - add returnLine 7 ;RETURNLINE = 4+7.
6 - push returnLine ;PUSH 11.
7 - push base ;FIRST PARAMETER.
8 - push result ;SECOND PARAMETER.
9 - push exponent ;THIRD PARAMETER.
10 - jump 36 ;FUNCTION CALL.
...
;POWER FUNCTION.
36 - pop exponent ;THIRD PARAMETER.
37 - pop result ;SECOND PARAMETER.
38 - pop base ;FIRST PARAMETER.
39 - jumpz 49 exponent ;FINISH IF EXPONENT IS ZERO.
40 - mul result base ;RESULT = ( RESULT * BASE ).
41 - add exponent -1 ;RECURSIVE CONTROL VARIABLE.
42 - set returnLine IP ;IP = 42.
43 - add returnLine 7 ;RETURN LINE = 42+7.
44 - push returnLine ;PUSH 49.
45 - push base
46 - push result
47 - push exponent
48 - jump 36 ;RECURSIVE CALL.
49 - pop returnLine
50 - jump returnLine
;POWER END.
Your asm does provide enough facilities to implement the usual procedure call / return sequence. You can push a return address and jump as a call, and pop a return address (into a scratch location) and do an indirect jump to it as a ret. We can just make call and ret macros. (Except that generating the correct return address is tricky in a macro; we might need a label (push ret_addr), or something like set tmp, IP / add tmp, 4 / push tmp / jump target_function). In short, it's possible and we should wrap it up in some syntactic sugar so we don't get bogged down with that while looking at recursion.
With the right syntactic sugar, you can implement Fibonacci(n) in assembly that will actually assemble for both x86 and your toy machine.
You're thinking in terms of functions that modify static (global) variables. Recursion requires local variables so each nested call to the function has its own copy of local variables. Instead of having registers, your machine has (apparently unlimited) named static variables (like x and y). If you want to program it like MIPS or x86, and copy an existing calling convention, just use some named variables like eax, ebx, ..., or r0 .. r31 the way a register architecture uses registers.
Then you implement recursion the same way you do in a normal calling convention, where either the caller or callee use push / pop to save/restore a register on the stack so it can be reused. Function return values go in a register. Function args should go in registers. An ugly alternative would be to push them after the return address (creating a caller-cleans-the-args-from-the-stack calling convention), because you don't have a stack-relative addressing mode to access them the way x86 does (above the return address on the stack). Or you could pass return addresses in a link register, like most RISC call instructions (usually called bl or similar, for branch-and-link), instead of pushing it like x86's call. (So non-leaf callees have to push the incoming lr onto the stack themselves before making another call)
A (silly and slow) naively-implemented recursive Fibonacci might do something like:
int Fib(int n) {
if(n<=1) return n; // Fib(0) = 0; Fib(1) = 1
return Fib(n-1) + Fib(n-2);
}
## valid implementation in your toy language *and* x86 (AMD64 System V calling convention)
### Convenience macros for the toy asm implementation
# pretend that the call implementation has some way to make each return_address label unique so you can use it multiple times.
# i.e. just pretend that pushing a return address and jumping is a solved problem, however you want to solve it.
%define call(target) push return_address; jump target; return_address:
%define ret pop rettmp; jump rettmp # dedicate a whole variable just for ret, because we can
# As the first thing in your program, set eax, 0 / set ebx, 0 / ...
global Fib
Fib:
# input: n in edi.
# output: return value in eax
# if (n<=1) return n; // the asm implementation of this part isn't interesting or relevant. We know it's possible with some adds and jumps, so just pseudocode / handwave it:
... set eax, edi and ret if edi <= 1 ... # (not shown because not interesting)
add edi, -1
push edi # save n-1 for use after the recursive call
call Fib # eax = Fib(n-1)
pop edi # restore edi to *our* n-1
push eax # save the Fib(n-1) result across the call
add edi, -1
call Fib # eax = Fib(n-2)
pop edi # use edi as a scratch register to hold Fib(n-1) that we saved earlier
add eax, edi # eax = return value = Fib(n-1) + Fib(n-2)
ret
During a recursive call to Fib(n-1) (with n-1 in edi as the first argument), the n-1 arg is also saved on the stack, to be restored later. So each function's stack frame contains the state that needs to survive the recursive call, and a return address. This is exactly what recursion is all about on a machine with a stack.
Jose's example doesn't demonstrate this as well, IMO, because no state needs to survive the call for pow. So it just ends up pushing a return address and args, then popping the args, building up just some return addresses. Then at the end, follows the chain of return addresses. It could be extended to save local state across each nested call, doesn't actually illustrate it.
My implementation is a bit different from how gcc compiles the same C function for x86-64 (using the same calling convention of first arg in edi, ret value in eax). gcc6.1 with -O1 keeps it simple and actually does two recursive calls, as you can see on the Godbolt compiler explorer. (-O2 and especially -O3 do some aggressive transformations). gcc saves/restores rbx across the whole function, and keeps n in ebx so it's available after the Fib(n-1) call. (and keeps Fib(n-1) in ebx to survive the second call). The System V calling convention specifies rbx as a call-preserved register, but rbi as call-clobbered (and used for arg-passing).
Obviously you can implement Fib(n) much faster non-recursively, with O(n) time complexity and O(1) space complexity, instead of O(Fib(n)) time and space (stack usage) complexity. It makes a terrible example, but it is trivial.
Margaret's pastebin modified slightly to run in my interpreter for this language: (infinite loop problem, probably due to a transcription error on my part)
set n 3
push n
set initialCallAddress IP
add initialCallAddress 4
push initialCallAddress
jump fact
set finalValue 0
pop finalValue
print finalValue
jump 100
:fact
set rip 0
pop rip
pop n
push rip
set temp n
add n -1
jumpz end n
push n
set link IP
add link 4
push link
jump fact
pop n
mul temp n
:end
pop rip
push temp
jump rip
Successful transcription of Peter's Fibonacci calculator:
String[] x = new String[] {
//n is our input, which term of the sequence we want to calculate
"set n 5",
//temp variable for use throughout the program
"set temp 0",
//call fib
"set temp IP",
"add temp 4",
"push temp",
"jump fib",
//program is finished, prints return value and jumps to end
"print returnValue",
"jump end",
//the fib function, which gets called recursively
":fib",
//if this is the base case, then we assert that f(0) = f(1) = 1 and return from the call
"jumple base n 1",
"jump notBase",
":base",
"set returnValue n",
"pop temp",
"jump temp",
":notBase",
//we want to calculate f(n-1) and f(n-2)
//this is where we calculate f(n-1)
"add n -1",
"push n",
"set temp IP",
"add temp 4",
"push temp",
"jump fib",
//return from the call that calculated f(n-1)
"pop n",
"push returnValue",
//now we calculate f(n-2)
"add n -1",
"set temp IP",
"add temp 4",
"push temp",
"jump fib",
//return from call that calculated f(n-2)
"pop n",
"add returnValue n",
//this is where the fib function ultimately ends and returns to caller
"pop temp",
"jump temp",
//end label
":end"
};

xcode - how do I remove an item from an array within an array?

I wish to remove an item from in an array that is nested in another array but try as I might I can't get the code to work.
This is what I have in my code:
NSArray *array1 =[[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",nil];
NSArray *array2 =[[NSArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",nil];
NSArray *array3 =[[NSArray alloc] initWithObjects:#"10",#"9",#"8",#"7",#"6",nil];
NSMutableArray *arrayOfArrays = [[NSMutableArray alloc] initWithObjects:array1,array2,array3, nil];
I then use NSLog to show which item I want to remove.
NSLog(#"Array item 1 position 3 = %#", [[arrayOfArrays objectAtIndex:1] objectAtIndex:3]);
This returns "d" which is correct.
So now I want to delete that item so am trying to use the following code:
NSMutableArray *subArray = [[NSMutableArray alloc] initWithObjects:arrayOfArrays [1], nil];
[[subArray objectAtIndex:0] removeObjectAtIndex:3];
When it runs and hits the above line I get the following:
2013-05-22 14:19:01.070 Arrays Demo[6958:303] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
(
0 CoreFoundation 0x00007fff8e9d0b06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff922953f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8e96d8ec -[__NSArrayM objectAtIndex:] + 252
3 Arrays Demo 0x0000000100001ca8 main + 1592
4 libdyld.dylib 0x00007fff8e8127e1 start + 0
5 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminate called throwing an exception
What am I doing wrong?
HELP!!!!!!!
array1, array2, array3 are objects of NSArray. So you couldn't remove elements in those arrays. You should create these arrays as objects of NSMutableArray.

Xcode - differences between simulator and devices on file read

I have tested my app on simulators 4.3, 5.0, 5.1 and 6.0 for iPod, iPad an iPhone. It's working as expected. But on all my devices it doesn't work.
I know that on devices the filenames are case-sensitive. Both the used constant DATA_FILE and the type are lowercase, and the file handle is not nil after opening.
As I can see, there is no actual reading after the proper file positioning. Why not?
This is the code, reduced to the minimum to show the problem.
NSLog(#"getData, index = %lu", index);
NSFileHandle *fhIndex = [NSFileHandle fileHandleForReadingAtPath: [[NSBundle mainBundle] pathForResource: DATA_FILE ofType:#"idx"]];
u_char indexData[4];
NSLog(#"index*4 = %lu", (index * 4));
[fhIndex seekToFileOffset: (index * 4)];
unsigned long long test = [fhIndex offsetInFile];
NSLog(#"FilePos = %llu",test);
[[fhIndex readDataOfLength: 4] getBytes: indexData length: 4];
test = [fhIndex offsetInFile];
NSLog(#"FilePos = %llu",test);
NSLog(#"indexData = %i %i %i %i", indexData[0], indexData[1], indexData[2], indexData[3]);
An index (unsigned long) is given to the routine, in this case index=62825. The program make a seek to position index*4 and then reads the next four bytes. On devices, these four bytes are not read!
Debugger output in the simulator:
2012-10-31 10:46:24.557 program[469:12003] getData, index = 62825
2012-10-31 10:46:24.559 program[469:12003] index*4 = 251300
2012-10-31 10:46:24.559 program[469:12003] FilePos = 251300
2012-10-31 10:46:24.580 program[469:12003] FilePos = 251304
2012-10-31 10:46:24.581 program[469:12003] indexData = 0 73 130 174
... and on the devices:
2012-10-31 10:41:57.776 program[2942:907] getData, index = 62825
2012-10-31 10:41:57.780 program[2942:907] index*4 = 251300
2012-10-31 10:41:57.782 program[2942:907] FilePos = 251300
2012-10-31 10:41:57.783 program[2942:907] FilePos = 251300
2012-10-31 10:41:57.784 program[2942:907] indexData = 248 10 18 0
As you can see in the output on devices, the file position is not changed after the read, that means, there was actually no read!
Wrong filesize has guided me in the right direction.
After deleting the Build directory the program is now working on simulators and devices.

Resources