This repository was archived by the owner on Dec 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathInstabugExampleMethodCallHandler.m
More file actions
125 lines (98 loc) · 3.71 KB
/
InstabugExampleMethodCallHandler.m
File metadata and controls
125 lines (98 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#import <Foundation/Foundation.h>
#import "InstabugExampleMethodCallHandler.h"
#import <InstabugSDK/IBGCrashReporting.h>
#import <InstabugSDK/InstabugSDK.h>
#import <Flutter/Flutter.h>
// MARK: - Private Interface
@interface InstabugExampleMethodCallHandler()
@property (nonatomic, strong) NSMutableArray *oomBelly;
@property (nonatomic, strong) dispatch_queue_t serialQueue;
@end
// MARK: - Constants
extern NSString * const kSendNativeNonFatalCrashMethod;
extern NSString * const kSendNativeFatalCrashMethod;
extern NSString * const kSendNativeFatalHangMethod;
extern NSString * const kSendOOMMethod;
extern NSString * const kInstabugChannelName;
// MARK: - MethodCallHandler Implementation
@implementation InstabugExampleMethodCallHandler
NSString * const kSendNativeNonFatalCrashMethod = @"sendNativeNonFatalCrash";
NSString * const kSendNativeFatalCrashMethod = @"sendNativeFatalCrash";
NSString * const kSendNativeFatalHangMethod = @"sendNativeFatalHang";
NSString * const kSendOOMMethod = @"sendOom";
NSString * const kInstabugChannelName = @"instabug_flutter_example";
// MARK: - Initializer
- (instancetype)init {
self = [super init];
if (self) {
self.serialQueue = dispatch_queue_create("QUEUE>SERIAL", DISPATCH_QUEUE_SERIAL);
}
return self;
}
// MARK: - Flutter Plugin Methods
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
- (void)oomCrash {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *belly = [NSMutableArray array];
while (true) {
// 20 MB chunks to speed up OOM
void *buffer = malloc(20 * 1024 * 1024);
if (buffer == NULL) {
NSLog(@"OOM: malloc failed");
break;
}
memset(buffer, 1, 20 * 1024 * 1024);
NSData *data = [NSData dataWithBytesNoCopy:buffer length:20 * 1024 * 1024 freeWhenDone:YES];
[belly addObject:data];
// Optional: slight delay to avoid CPU spike
[NSThread sleepForTimeInterval:0.01];
}
});
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([kSendNativeNonFatalCrashMethod isEqualToString:call.method]) {
NSLog(@"Sending native non-fatal crash from iOS");
[self sendNativeNonFatal:call.arguments];
result(nil);
} else if ([kSendNativeFatalCrashMethod isEqualToString:call.method]) {
NSLog(@"Sending native fatal crash from iOS");
[self sendNativeFatalCrash];
result(nil);
} else if ([kSendNativeFatalHangMethod isEqualToString:call.method]) {
NSLog(@"Sending native fatal hang for 3000 ms from iOS");
[self sendFatalHang];
result(nil);
} else if ([kSendOOMMethod isEqualToString:call.method]) {
NSLog(@"Sending out of memory from iOS");
[self sendOOM];
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
}
// MARK: - Helper Methods
- (void)sendNativeNonFatal:(NSString *)exceptionObject {
IBGNonFatalException *nonFatalException = [IBGCrashReporting exception:[NSException exceptionWithName:@"native Handled NS Exception" reason:@"Test iOS Handled Crash" userInfo:@{@"Key": @"Value"}]];
[nonFatalException report];
}
- (void)sendNativeFatalCrash {
NSException *exception = [NSException exceptionWithName:@"native Unhandled NS Exception" reason:@"Test iOS Unhandled Crash" userInfo:nil];
@throw exception;
}
- (void)sendFatalHang {
dispatch_async(dispatch_get_main_queue(), ^{
sleep(20); // Block main thread for 20 seconds
});
}
- (void)sendOOM {
[self oomCrash];
}
- (void)sendNativeNonFatal {
}
@end