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

fix: resolve crashes caused by using the ffmpeg library #580

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

julee
Copy link

@julee julee commented Jul 17, 2024

  • Addressed issue where repeated reloading of the ffmpeg library led to crashes. Previously, each component attempted to load the ffmpeg library and executed an unload before loading, causing other components to retain stale function addresses if the library was reloaded at a different address. This issue was particularly prevalent on macOS.
  • Fixed undefined behavior due to incorrect size assumption of std::function objects. On certain platforms like arm64 macOS, sizeof(std::function<void()>) is 32, not the same as a pointer size. This discrepancy led to undefined behavior when forcibly casting.

This fix ensures that the library is loaded only once per process and corrects the handling of std::function objects to prevent crashes.

- Addressed issue where repeated reloading of the ffmpeg library led to crashes. Previously, each component attempted to load the ffmpeg library and executed an unload before loading, causing other components to retain stale function addresses if the library was reloaded at a different address. This issue was particularly prevalent on macOS.
- Fixed undefined behavior due to incorrect size assumption of std::function objects. On certain platforms like arm64 macOS, sizeof(std::function<void()>) is 32, not the same as a pointer size. This discrepancy led to undefined behavior when forcibly casting.

This fix ensures that the library is loaded only once per process and corrects the handling of std::function objects to prevent crashes.
@ChristianFeldmann
Copy link
Member

Hi! Thanks for the report. I have not heard of this undefined behavior so far. Do you have a link to this? Maybe an article or a bugreport?
In the next change, I will replace the ffmpeg integration with https://github.com/ChristianFeldmann/LibFFmpeg
. That is essentially the code from here extracted into a separate tested library. In that other repository I have added many tests on many different platforms including M1 and I have not seen any issues with is there.

@julee
Copy link
Author

julee commented Aug 12, 2024

The core reason for the crash is that after unloading the dynamic library, the original symbol addresses are still being used. This is not undefined behavior, but a clear bug!

consider code:

//compoent A:
QLibrary lib;
lib.setFileName(libAVCodecPath);
lib.unload(); //call by unloadAllLibraries
lib.load();
void *fucntionPtr1 =  lib.resolve(symbolNameSuchAsAVCodecOpen);

//compoent B:
QLibrary lib;
lib.setFileName(libAVCodecPath);
// as shared library loading is global for one process, so here, lib.unload() will cause libAVCodecPath loaded in compoent A unload!
lib.unload(); //call by unloadAllLibraries
lib.load();
void *fucntionPtr2 =  lib.resolve(symbolNameSuchAsAVCodecOpen); // this address may be different with fucntionPtr1

application go on run:
compoentA.runXXXUse_fucntionPtr1();   // go on use: functionPtr1, but this address is invalid,

@julee
Copy link
Author

julee commented Oct 23, 2024

Can you continue to evaluate this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants