Skip to content

Commit 067ac52

Browse files
armcknightphilipphofmann
authored andcommitted
ref: move SentryCrash singleton to SentryDependencyContainer (#3247)
1 parent e629d90 commit 067ac52

19 files changed

+73
-62
lines changed

SentryTestUtils/ClearTestState.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ class TestCleanup: NSObject {
4848
PrivateSentrySDKOnly.onAppStartMeasurementAvailable = nil
4949
SentrySDK.setAppStartMeasurement(nil)
5050
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
51+
52+
sentrycrash_scopesync_reset()
5153
}
5254
}

SentryTestUtils/SentryTestUtils-ObjC-BridgingHeader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#import "SentryReachability.h"
4343
#import "SentrySDK+Private.h"
4444
#import "SentrySDK+Tests.h"
45+
#import "SentryScopeSyncC.h"
4546
#import "SentrySession.h"
4647
#import "SentrySwizzleWrapper.h"
4748
#import "SentrySystemWrapper.h"

Sources/Sentry/SentryCrashExceptionApplication.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "SentryCrashExceptionApplication.h"
22
#import "SentryCrash.h"
3+
#import "SentryDependencyContainer.h"
34
#import "SentrySDK.h"
45

56
@implementation SentryCrashExceptionApplication
@@ -10,8 +11,9 @@ - (void)reportException:(NSException *)exception
1011
{
1112
[[NSUserDefaults standardUserDefaults]
1213
registerDefaults:@{ @"NSApplicationCrashOnExceptions" : @YES }];
13-
if (nil != SentryCrash.sharedInstance.uncaughtExceptionHandler && nil != exception) {
14-
SentryCrash.sharedInstance.uncaughtExceptionHandler(exception);
14+
SentryCrash *crash = SentryDependencyContainer.sharedInstance.crashReporter;
15+
if (nil != crash.uncaughtExceptionHandler && nil != exception) {
16+
crash.uncaughtExceptionHandler(exception);
1517
}
1618
[super reportException:exception];
1719
}

Sources/Sentry/SentryCrashIntegration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ - (void)configureScope
185185
userInfo[@"release"] = self.options.releaseName;
186186
userInfo[@"dist"] = self.options.dist;
187187

188-
[SentryCrash.sharedInstance setUserInfo:userInfo];
188+
[SentryDependencyContainer.sharedInstance.crashReporter setUserInfo:userInfo];
189189

190190
[outerScope addObserver:self.scopeObserver];
191191
}];

Sources/Sentry/SentryCrashWrapper.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import <SentryCrashCachedData.h>
88
#import <SentryCrashDebug.h>
99
#import <SentryCrashMonitor_System.h>
10+
#import <SentryDependencyContainer.h>
1011
#include <mach/mach.h>
1112

1213
NS_ASSUME_NONNULL_BEGIN
@@ -23,7 +24,7 @@ + (instancetype)sharedInstance
2324

2425
- (BOOL)crashedLastLaunch
2526
{
26-
return SentryCrash.sharedInstance.crashedLastLaunch;
27+
return SentryDependencyContainer.sharedInstance.crashReporter.crashedLastLaunch;
2728
}
2829

2930
- (NSTimeInterval)durationFromCrashStateInitToLastCrash
@@ -33,7 +34,7 @@ - (NSTimeInterval)durationFromCrashStateInitToLastCrash
3334

3435
- (NSTimeInterval)activeDurationSinceLastCrash
3536
{
36-
return SentryCrash.sharedInstance.activeDurationSinceLastCrash;
37+
return SentryDependencyContainer.sharedInstance.crashReporter.activeDurationSinceLastCrash;
3738
}
3839

3940
- (BOOL)isBeingTraced
@@ -55,7 +56,8 @@ - (NSDictionary *)systemInfo
5556
{
5657
static NSDictionary *sharedInfo = nil;
5758
static dispatch_once_t onceToken;
58-
dispatch_once(&onceToken, ^{ sharedInfo = SentryCrash.sharedInstance.systemInfo; });
59+
dispatch_once(&onceToken,
60+
^{ sharedInfo = SentryDependencyContainer.sharedInstance.crashReporter.systemInfo; });
5961
return sharedInfo;
6062
}
6163

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "SentryUIDeviceWrapper.h"
1616
#import <SentryAppStateManager.h>
1717
#import <SentryClient+Private.h>
18+
#import <SentryCrash.h>
1819
#import <SentryCrashWrapper.h>
1920
#import <SentryDebugImageProvider.h>
2021
#import <SentryDependencyContainer.h>
@@ -121,6 +122,18 @@ - (SentryCrashWrapper *)crashWrapper
121122
return _crashWrapper;
122123
}
123124

125+
- (SentryCrash *)crashReporter
126+
{
127+
if (_crashReporter == nil) {
128+
@synchronized(sentryDependencyContainerLock) {
129+
if (_crashReporter == nil) {
130+
_crashReporter = [[SentryCrash alloc] init];
131+
}
132+
}
133+
}
134+
return _crashReporter;
135+
}
136+
124137
- (SentrySysctl *)sysctlWrapper
125138
{
126139
if (_sysctlWrapper == nil) {

Sources/Sentry/SentrySDK.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ + (void)setUser:(SentryUser *_Nullable)user
338338

339339
+ (BOOL)crashedLastRun
340340
{
341-
return SentryCrash.sharedInstance.crashedLastLaunch;
341+
return SentryDependencyContainer.sharedInstance.crashReporter.crashedLastLaunch;
342342
}
343343

344344
+ (void)startSession

Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@class SentryANRTracker;
44
@class SentryAppStateManager;
55
@class SentryBinaryImageCache;
6+
@class SentryCrash;
67
@class SentryCrashWrapper;
78
@class SentryCurrentDateProvider;
89
@class SentryDebugImageProvider;
@@ -54,6 +55,7 @@ SENTRY_NO_INIT
5455
@property (nonatomic, strong) SentryFileManager *fileManager;
5556
@property (nonatomic, strong) SentryAppStateManager *appStateManager;
5657
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
58+
@property (nonatomic, strong) SentryCrash *crashReporter;
5759
@property (nonatomic, strong) SentryThreadWrapper *threadWrapper;
5860
@property (nonatomic, strong) id<SentryRandom> random;
5961
@property (nonatomic, strong) SentrySwizzleWrapper *swizzleWrapper;

Sources/SentryCrash/Installations/SentryCrashInstallation.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "SentryCrashJSONCodecObjC.h"
3434
#import "SentryCrashLogger.h"
3535
#import "SentryCrashReportFilterBasic.h"
36+
#import "SentryDependencyContainer.h"
3637
#import <objc/runtime.h>
3738

3839
/** Max number of properties that can be defined for writing to the report */
@@ -175,7 +176,7 @@ - (id)initWithRequiredProperties:(NSArray *)requiredProperties
175176

176177
- (void)dealloc
177178
{
178-
SentryCrash *handler = [SentryCrash sharedInstance];
179+
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
179180
@synchronized(handler) {
180181
if (g_crashHandlerData == self.crashHandlerData) {
181182
g_crashHandlerData = NULL;
@@ -260,7 +261,7 @@ - (void)setOnCrash:(SentryCrashReportWriteCallback)onCrash
260261

261262
- (void)install
262263
{
263-
SentryCrash *handler = [SentryCrash sharedInstance];
264+
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
264265
@synchronized(handler) {
265266
g_crashHandlerData = self.crashHandlerData;
266267
handler.onCrash = crashCallback;
@@ -270,7 +271,7 @@ - (void)install
270271

271272
- (void)uninstall
272273
{
273-
SentryCrash *handler = [SentryCrash sharedInstance];
274+
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
274275
@synchronized(handler) {
275276
if (g_crashHandlerData == self.crashHandlerData) {
276277
g_crashHandlerData = NULL;
@@ -302,7 +303,7 @@ - (void)sendAllReportsWithCompletion:(SentryCrashReportFilterCompletion)onComple
302303

303304
sink = [SentryCrashReportFilterPipeline filterWithFilters:self.prependedFilters, sink, nil];
304305

305-
SentryCrash *handler = [SentryCrash sharedInstance];
306+
SentryCrash *handler = SentryDependencyContainer.sharedInstance.crashReporter;
306307
handler.sink = sink;
307308
[handler sendAllReportsWithCompletion:onCompletion];
308309
}

Sources/SentryCrash/Recording/Monitors/SentryCrashMonitor_MachException.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151

5252
# ifdef __LP64__
5353
# define MACH_ERROR_CODE_MASK 0xFFFFFFFFFFFFFFFF
54-
# else
54+
# else // !__LP64__
5555
# define MACH_ERROR_CODE_MASK 0xFFFFFFFF
56-
# endif
56+
# endif // __LP64__
5757

5858
// ============================================================================
5959
# pragma mark - Types -
@@ -267,7 +267,7 @@ sentrycrashcm_hasReservedThreads(void)
267267
return g_primaryMachThread != 0 && g_secondaryMachThread != 0;
268268
}
269269

270-
#else
270+
#else // !SentryCrashCRASH_HAS_MACH
271271
bool
272272
sentrycrashcm_isReservedThread(thread_t thread)
273273
{
@@ -280,7 +280,7 @@ sentrycrashcm_hasReservedThreads(void)
280280
return false;
281281
}
282282

283-
#endif
283+
#endif // SentryCrashCRASH_HAS_MACH
284284

285285
#if SentryCrashCRASH_HAS_MACH
286286

@@ -560,7 +560,7 @@ addContextualInfoToEvent(struct SentryCrash_MonitorContext *eventContext)
560560
}
561561
}
562562

563-
#endif
563+
#endif // SentryCrashCRASH_HAS_MACH
564564

565565
SentryCrashMonitorAPI *
566566
sentrycrashcm_machexception_getAPI(void)
@@ -570,7 +570,7 @@ sentrycrashcm_machexception_getAPI(void)
570570
.setEnabled = setEnabled,
571571
.isEnabled = isEnabled,
572572
.addContextualInfoToEvent = addContextualInfoToEvent
573-
#endif
573+
#endif // SentryCrashCRASH_HAS_MACH
574574
};
575575
return &api;
576576
}

0 commit comments

Comments
 (0)