Skip to content

Commit f5c9ec1

Browse files
authored
Release xrView related memory when xr session stops (iOS) (#246)
1 parent 3049774 commit f5c9ec1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Modules/@babylonjs/react-native/ios/EngineViewManager.mm

+15-10
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ - (instancetype)init:(RCTBridge*)_bridge {
2626
super.translatesAutoresizingMaskIntoConstraints = false;
2727
super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
2828
super.depthStencilPixelFormat = MTLPixelFormatDepth32Float;
29-
30-
xrView = [[MTKView alloc] initWithFrame:self.bounds device:self.device];
31-
xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
32-
xrView.userInteractionEnabled = false;
33-
xrView.hidden = true;
34-
[self addSubview:xrView];
35-
[BabylonNativeInterop updateXRView:xrView];
3629
}
3730
return self;
3831
}
@@ -60,14 +53,26 @@ - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
6053

6154
- (void)drawRect:(CGRect)rect {
6255
if ([BabylonNativeInterop isXRActive]) {
63-
xrView.hidden = false;
64-
} else {
65-
xrView.hidden = true;
56+
if (!xrView) {
57+
xrView = [[MTKView alloc] initWithFrame:self.bounds device:self.device];
58+
xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
59+
xrView.userInteractionEnabled = false;
60+
[self addSubview:xrView];
61+
[BabylonNativeInterop updateXRView:xrView];
62+
}
63+
} else if (xrView) {
64+
[BabylonNativeInterop updateXRView:nil];
65+
[xrView removeFromSuperview];
66+
xrView = nil;
6667
}
6768

6869
[BabylonNativeInterop renderView];
6970
}
7071

72+
-(void)dealloc {
73+
[BabylonNativeInterop updateXRView:nil];
74+
}
75+
7176
- (void)takeSnapshot {
7277
// We must take the screenshot on the main thread otherwise we might fail to get a valid handle on the view's image.
7378
dispatch_async(dispatch_get_main_queue(), ^{

0 commit comments

Comments
 (0)