Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashs since Xcode 14.3 compilation CIContext#render. #1175

Closed
Goule opened this issue Apr 7, 2023 · 24 comments
Closed

Crashs since Xcode 14.3 compilation CIContext#render. #1175

Goule opened this issue Apr 7, 2023 · 24 comments
Labels
Milestone

Comments

@Goule
Copy link
Contributor

Goule commented Apr 7, 2023

Describe the bug

Hello,

Since I compiled my app with Xcode 14.3 I've noticed some crashes:

Screenshot 2023-04-07 at 09 14 31

Screenshot 2023-04-07 at 09 21 45

It's hard to put code here since there are a lot of contexts with the app. But if someone has had the same issue since the Xcode 14.3 compilation maybe it can help.

To Reproduce

  1. It appear for some user when the stream start or randomly
  2. We have some simple video effect with images
  3. We are pushing sample buffer from the camera

Expected behavior

No crash

Version

  • HaishinKit 1.3.0 (we are not up to date so maybe it's fixed in the 1.4.5 ?)

Smartphone info.

  • iOS 16.4, 16.3 and 16.1
  • Xcode 14.3

Additional context

No response

Screenshots

No response

Relevant log output

No response

@shogo4405
Copy link
Owner

I have heard that Xcode 14.3 has bugs. Even if you update to 14.5, I think crashes will still occur.
Can I see “Données” tab? I want to know model names and free RAM size. By the way, are you using the patch for #986?

@Goule
Copy link
Contributor Author

Goule commented Apr 8, 2023

I've downgraded my Xcode version and all seems to be ok. It's probably a bug with Xcode 14.3...

Yes we are using the #986 patch !

Here are some data :

Screenshot 2023-04-08 at 10 40 11

Screenshot 2023-04-08 at 10 40 14

Screenshot 2023-04-08 at 10 40 17

Screenshot 2023-04-08 at 10 40 20

Screenshot 2023-04-08 at 10 40 23

Screenshot 2023-04-08 at 10 40 26

Screenshot 2023-04-08 at 10 40 29

Screenshot 2023-04-08 at 10 40 36

Screenshot 2023-04-08 at 10 40 41

@shogo4405
Copy link
Owner

There may be a possibility that the detailed behavior of DispatchQueue has changed for situations with low memory capacity. Since all of them have low memory capacity, I'm concerned about the possibility of memory leaks occurring in Xcode 14.3.

I will investigate. thank you.

@Goule
Copy link
Contributor Author

Goule commented Apr 8, 2023

Thanks @shogo4405 !

@shogo4405
Copy link
Owner

I'm having difficulty reproducing the issue. I believe that the application crashes due to limited available memory, so I recommend that you limit memory usage on the application side to prevent this.
If it's a sample app, it uses around 110MB of memory. We haven't confirmed any memory leaks on the framework side.

Please let me know if there are other people who have the same issue.

@heestand-xyz
Copy link

heestand-xyz commented May 17, 2023

I'm experiencing a similar issue. Crashes here and here.
The devices were low memory when the EXC_BAD_ACCESS happened.
Xcode 14.3 on iOS 16.1.1 & 16.2.0 with HaishinKit 1.4.2

@Goule
Copy link
Contributor Author

Goule commented May 18, 2023

Xcode 14.3.1 is out, hope it will fix it !

@Goule
Copy link
Contributor Author

Goule commented Jun 8, 2023

The issue still here with XCode 14.3.1 :(

@floriangbh
Copy link

Hello,

I have the same issue, but hard to reproduce, there is no tool to overhead the memory of the device...

Did you manage to reproduce this ?

@leo150
Copy link
Contributor

leo150 commented Jul 19, 2023

Just a guess to figure out the source, maybe try to use no optimization for target's Build Settings - Swift Compiler - Optimization Level? And test it on small selection of user base.

@leo150
Copy link
Contributor

leo150 commented Jul 19, 2023

Recalling a similar issue that happened on iOS 15 (?). There was a bug when user connects headphones, this would make the encoding to freeze for a few seconds, while appendSampleBuffer keeps executing until there's no memory. So the workaround was to wait until videoIO finishes encoding:

if self.limit.value > someNumber {
    return
}

self.limit.mutate { $0 += 1 }

mixer.videoIO.lockQueue.async {
    self.mixer.videoIO.encodeSampleBuffer(sampleBuffer)
    
    self.limit.mutate { $0 -= 1 }
}

@floriangbh
Copy link

Thanks @leo150 for the research lead, I'll try. I admit that I was waiting to see under Xcode 15 if this fixes anything..

@leo150
Copy link
Contributor

leo150 commented Sep 29, 2023

Does anyone have luck with XCode 15? I can't compile with 14.2 anymore because of the recent changes.

@floriangbh
Copy link

Also waiting for feedbacks with Xcode 15 😅

@shogo4405
Copy link
Owner

Oops! The app being killed due to OOM (Out of Memory) when it exceeds device limits is an iOS specification. I believe it's up to the application to control memory usage and ensure it doesn't exceed the limits. Is everyone pausing xcode updates for now?

@floriangbh
Copy link

The weird things is that OOM appear since Xcode 14.3 compilation...
I haven't tested yet with 15.0, maybe in november with progressive release...

@Goule
Copy link
Contributor Author

Goule commented Oct 26, 2023

Hello,

I've just tested on XCode 15.0.1.
Same issue with HaishinKit v1.6.1, crash appear always with low memory.
I don't know how to investigate on it..

@shogo4405 do you think it's leak on our side? Or in the library?

HaishinKit and Sonoma aren't compatible with XCode 14.2 so I can't use it to build :(

Technical details : I'm using self.rtmpStream.appendSampleBuffer(frame) to push frame from custom AVCaptureVideoDataOutputSampleBufferDelegate

@heestand-xyz did you fix the issue on your side ?

@floriangbh
Copy link

floriangbh commented Oct 27, 2023

In my case the crash appear only in release mode, with GCC_OPTIMIZATION_LEVEL = s and SWIFT_OPTIMIZATION_LEVEL = "-O", only when the available memory is low. Really quickly after the controller with the NetStream and preview appear.

I tried to figure out if there is a memory leak with the profiler, but all seem to be stable even in release mode.

The crash appears here :

Screenshot 2023-10-27 at 12 57 07

I'm also using appendSampleBuffer instead of camera attached.

@shogo4405
Copy link
Owner

@Goule Goule In that usage, if you follow the next steps, you can save memory usage and reduce the chances of crashing due to memory shortage:

On the app side, set the following value:

  • Set the value to AVCaptureVideoDataOutput.alwaysDiscardsLateVideoFrames = true.

For HaishinKit, you need to make the following adjustments:

  • Remove the section mixer.audioIO.lockQueue of mixer.videoIO.lockQueue in HaishinKit.
    • Link
    • When processing falls behind, data accumulates in these lockQueues, causing memory pressure.
  • Perform stream.appendSampleBuffer on the same queue as set with AVCaptureVideoDataOutput.setSampleBufferDelegate.

@shogo4405
Copy link
Owner

@floriangbh Is "attachScreen" for iOS or macOS?

@shogo4405 shogo4405 changed the title Crashs since Xcode 14.3 compilation Crashs since Xcode 14.3 compilation with NetStream.appendSampleBuffer method. Oct 28, 2023
@Goule
Copy link
Contributor Author

Goule commented Oct 28, 2023

Thanks for your help @shogo4405 !

I will try with your recommendation.

Before that I've tested with rtmpStream.attachCamera, and still have the same issue. So it's no coming from the extra code around the appendSampleBuffer I think...

Screenshot 2023-10-28 at 09 13 02

I'll try to reproduce it with the sample project !

@shogo4405
Copy link
Owner

shogo4405 commented Oct 28, 2023

It is difficult to fix the issue of crashing due to memory capacity limits. I believe that what was previously undetectable as OOM can now be detected as crashes through Firebase. I think it's better to come up with solutions to avoid reaching memory limits rather than trying to fix it afterwards.

I'm adding code to the example that deliberately leads to memory shortage. It uses MemoryUsage.toEmpty() to use up memory without causing an OOM (Out of Memory) situation to that extent.
https://github.com/shogo4405/HaishinKit.swift/blob/main/Examples/iOS/MemoryUsage.swift#L15-L18

@Goule
Copy link
Contributor Author

Goule commented Oct 28, 2023

I'm not sure this is an OOM :

  • the application uses only 130/150Mb
  • an OOM is easily detectable with Xcode, after the application closes a message appears with "...has been killed by the operating system because it is using too much memory."

It look more like a memory corruption with a bad access to unaloccated object or something else.

I can reproduce it with Xcode, the memory when the app crash seem to be ok, with 750MB free :
Screenshot 2023-10-28 at 17 41 05

I will try to reduce the scope of my app with only simple screen to isolate the crash.

@shogo4405 shogo4405 changed the title Crashs since Xcode 14.3 compilation with NetStream.appendSampleBuffer method. Crashs since Xcode 14.3 compilation @CIContext.render. Nov 1, 2023
@shogo4405 shogo4405 changed the title Crashs since Xcode 14.3 compilation @CIContext.render. Crashs since Xcode 14.3 [email protected]. Nov 1, 2023
@shogo4405 shogo4405 changed the title Crashs since Xcode 14.3 [email protected]. Crashs since Xcode 14.3 compilation CIContext#render. Nov 1, 2023
@shogo4405
Copy link
Owner

I can reproduce it.

Prerequisites

  • GCC_OPTIMIZATION_LEVEL = s
  • WIFT_OPTIMIZATION_LEVEL = "-O"
  • Prepare two MTHKViews. And stream.attachCamera, stream.attachAudio

Reproduce

  1. mthkView1.attachStream(stream)
  2. mthkView1.attachStream(nil)
  3. mthkView2.attachStream(stream)
    1. Crash CIContext#render method.

How to fix it.

https://github.com/shogo4405/HaishinKit.swift/blob/main/Sources/Media/MTHKView.swift#L71

currentStream.mixer.videoIO.context = CIContext(mtlDevice: device!)
currentStream.mixer.videoIO.lockQueue.async {
  self.currentStream.mixer.videoIO.context = CIContext(mtlDevice: device!)
}

@shogo4405 shogo4405 added bug and removed help wanted labels Nov 2, 2023
@shogo4405 shogo4405 added this to the 1.6.2 milestone Nov 2, 2023
shogo4405 added a commit that referenced this issue Nov 2, 2023
shogo4405 added a commit that referenced this issue Nov 2, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants