Commit b669184
authored
Use a local strong NSError for methods with nested autorelease pool (#10465)
Otherwise, the out `NSError` arg get assigned an autoreleasing `NSError`
object that gets deallocated when the nested autorelease pool is drained
and no error is reported:
```objectivec
- (BOOL)foo:(NSError **)error {
if (error) {
// Creates an autoreleased error object.
*error = [NSError errorWithDomain:@"DemoDomain" code:42 userInfo:nil];
}
return NO;
}
- (BOOL)bar:(NSError **)error {
@autoreleasepool {
// foo: returns NO, sets *error to an autoreleased NSError.
// But as soon as we leave this block, that NSError is released.
return [self foo:error];
}
}
- (void)run {
NSError *error = nil;
if (![self bar:&error]) { // Crash here even before entering the if-statement below due to `objc_storeStrong` call on the autoreleased error out arg.
// Even if we manage to get here by marking the error var as __autoreleasing above,
// Error now points at freed memory, so we get another crash or garbage.
NSLog(@"❌ Faulty: error code = %ld", (long)error.code);
}
}
```
We introduce a local error var to hold onto the autoreleased `NSError`
created by some other API like `foo` and use it to initialize the out
`NSError` arg:
```objectivec
- (BOOL)bar:(NSError **)error {
NSError *localError = nil;
BOOL ok = NO;
@autoreleasepool {
ok = [self foo:&localError];
// ARC retains localError here when assigning the autoreleased object.
}
if (!ok && error) {
*error = localError; // Safe - localError survived the pool drain.
}
return ok;
}
- (void)run {
NSError *error = nil;
if (![self bar:&error]) {
if (error) { // Don't forget to still check the error var.
// Now this reliably logs “42”
NSLog(@"✅ Fixed: error code = %ld", (long)error.code);
}
}
}
```
Another important observation is that generally it's required to check
if the out `NSError` has been set despite the returned status.
And a more trickier crash happens even before the control reaches error
handling. The compiler assumes the error local var in the `run` method
is `__strong`, so it generates `objc_storeStrong` call. Whereas in fact
it points to an `__autoreleasing` object (that gets deallocated when the
pool drains as we clarified), and thus such a call on it will lead to a
crash regardless, we won't even reach the code that tries to access
`code` on it. Unless we indeed introduce a local strong var inside `bar`
to be assigned to the out `NSError` arg and so make the generated
`objc_storeStrong` call a legit one. Although, that issue can
technically be resolved differently - by marking `error` var in `run` as
`__autoreleasing` to skip the `objc_storeStrong` call. But that still
doesn't help reporting the actual error, since it's gonna be nil after
the nested autorelease pool drainage inside `bar`. And apparently having
the nested autorelease pool is important enough, so we can't avoid it
and have to use such workarounds.1 parent f3e8972 commit b669184
File tree
4 files changed
+98
-79
lines changed- backends/apple/coreml/runtime
- delegate
- sdk
4 files changed
+98
-79
lines changedLines changed: 48 additions & 45 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
711 | 711 | | |
712 | 712 | | |
713 | 713 | | |
| 714 | + | |
714 | 715 | | |
715 | 716 | | |
716 | 717 | | |
717 | 718 | | |
718 | 719 | | |
719 | | - | |
| 720 | + | |
720 | 721 | | |
721 | | - | |
| 722 | + | |
722 | 723 | | |
723 | 724 | | |
724 | 725 | | |
725 | 726 | | |
726 | 727 | | |
727 | 728 | | |
728 | 729 | | |
729 | | - | |
| 730 | + | |
730 | 731 | | |
| 732 | + | |
731 | 733 | | |
732 | 734 | | |
733 | 735 | | |
734 | 736 | | |
735 | 737 | | |
736 | 738 | | |
737 | 739 | | |
738 | | - | |
739 | 740 | | |
740 | 741 | | |
741 | 742 | | |
742 | 743 | | |
743 | 744 | | |
744 | | - | |
745 | | - | |
746 | | - | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
747 | 749 | | |
748 | | - | |
749 | | - | |
750 | 750 | | |
751 | | - | |
752 | | - | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
753 | 757 | | |
754 | 758 | | |
755 | 759 | | |
756 | 760 | | |
757 | 761 | | |
758 | 762 | | |
759 | 763 | | |
| 764 | + | |
760 | 765 | | |
761 | 766 | | |
762 | 767 | | |
763 | 768 | | |
764 | 769 | | |
765 | | - | |
| 770 | + | |
766 | 771 | | |
767 | | - | |
768 | 772 | | |
769 | 773 | | |
770 | 774 | | |
771 | 775 | | |
772 | 776 | | |
773 | 777 | | |
774 | 778 | | |
775 | | - | |
| 779 | + | |
776 | 780 | | |
777 | | - | |
778 | 781 | | |
779 | 782 | | |
| 783 | + | |
780 | 784 | | |
781 | | - | |
782 | | - | |
783 | | - | |
784 | | - | |
785 | | - | |
786 | | - | |
787 | | - | |
788 | | - | |
789 | | - | |
790 | | - | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
796 | | - | |
797 | | - | |
798 | | - | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
803 | 809 | | |
804 | | - | |
805 | | - | |
806 | | - | |
807 | | - | |
808 | | - | |
809 | | - | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
810 | 814 | | |
811 | | - | |
812 | | - | |
813 | 815 | | |
| 816 | + | |
814 | 817 | | |
815 | 818 | | |
816 | 819 | | |
| |||
Lines changed: 18 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
110 | 112 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
119 | 117 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
124 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
125 | 127 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
130 | 133 | | |
131 | 134 | | |
132 | 135 | | |
| |||
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
| 141 | + | |
| 142 | + | |
142 | 143 | | |
143 | 144 | | |
144 | 145 | | |
| |||
150 | 151 | | |
151 | 152 | | |
152 | 153 | | |
153 | | - | |
| 154 | + | |
154 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
155 | 159 | | |
156 | 160 | | |
157 | 161 | | |
| |||
Lines changed: 26 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
661 | 661 | | |
662 | 662 | | |
663 | 663 | | |
| 664 | + | |
| 665 | + | |
664 | 666 | | |
665 | | - | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
666 | 671 | | |
| 672 | + | |
667 | 673 | | |
668 | 674 | | |
669 | 675 | | |
670 | 676 | | |
671 | 677 | | |
672 | 678 | | |
673 | 679 | | |
674 | | - | |
675 | | - | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
676 | 684 | | |
677 | | - | |
678 | | - | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
685 | | - | |
686 | | - | |
687 | | - | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
688 | 695 | | |
689 | 696 | | |
690 | | - | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
691 | 701 | | |
692 | | - | |
| 702 | + | |
693 | 703 | | |
694 | | - | |
695 | 704 | | |
696 | 705 | | |
697 | 706 | | |
| |||
0 commit comments