-
-
Notifications
You must be signed in to change notification settings - Fork 324
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
AccessViolationException calling avio_close #217
Comments
I don't see any difference in bindings for |
thanks for getting back to me. I found the issue, setting formatContext->pb->seekable = 1 breaks it now for some reason
|
i logged a ticket with ffmpeg and they said it's working fine on their side so possibly some issue with the wrapper? |
I see meanwhile can you try new packages #210 (comment)? DynamicallyLoaded is the closest to what we have now. In the very beginning you have to initialize the API then the rest is similar to what you have already: |
I'll test sniped you wrote. |
i can't test that update unfortunately as my project is .net standard 2.0 and i can't update that to 2.1 as that breaks compat with the 4.8 framework project that uses it. Is there a reason you're using the 2.1 version of net standard? It limits the usability of the library quite a lot. |
Well UnmanagedType.LPUTF8Str is in only in 2.1 standard - and this going to be newest version which supposed to support only net 6.0 as the rest is unsupported by MS any more. Custom marshaller - UTF8Marshaler can help here however in general I would prefer to drop anything lower than 2.1 standard. |
the full .net framework is still supported by MS Support lifecycle https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-framework |
Okay I'll add it to targets then. |
Hey @Ruslan-B did you manage to repro the original issue? |
Confirming same exception while using .NET 6.0.400 and GPL FFmpeg 5.1. Here is the log from 5.1
And working 4.4
It doesn't seems like bindings or marshaling - something different. |
Hi, I found different size of checksum in AVIOContext. So it has different offset after checksum.
FFmpeg.AutoGen/FFmpeg.AutoGen/FFmpeg.structs.g.cs Line 2060 in d7f6e23
|
@hglee I didn't get could you please elaborate? |
In FFmpeg.Autogen, size of checksum is 8 bytes (ulong = uint64). But in native version, size of checksum is 4 bytes in common case (unsigned long = uint32). So it has different offset in managed version and if you write seekable field in managed, it corrupts memory around seekable field (maybe protocol_whitelist ?). So it crashes in avio_close, actually, in av_freep (avio_close -> av_opt_free -> av_freep). |
@hglee it is actually brilliant analysis - we have to re-test - this any way - but I am scratching my head - seems it has to be hinted to solve this - but how many this kind of thing we have at the moment- I think report back to FFmpeg - but again it has to be a collective claim as they wave it out otherwise. |
To make it clear - it is Clang - without any standard so it will work like a magic as long you on platform which can compile it for years and then it breaks))) |
I am not convinced - as double checked - as in 4.4 we have as well - it is possible to redefine ulong in Clang, but why you should - it is 64 bit world not 4 bytes - if it is - then is a bug in ffmpeg - it used to work in 4.4
|
In 4.x, there was no critical fields around seekable in my guess.
And you need to consider size of unsigned long by platforms. In .NET, ulong is always 8 bytes. In Windows x86/x64 native, unsigned long is 4 bytes in common case. But other platforms like Linux, OSX x86/x64 have different size model. |
I know -but I am don't buy it as it would be a change for everting - the rest is working - I don't see it in headers - don't say it isn't the case - but what wise to do here disable structure alignment first... |
offtopic |
@Ruslan-B I can confirm (for Windows) that by changing the checksum from ulong to UInt32 (uint) resolves the issue (all the next attributes read properly). I remember even with v4 having an issue when I was using HLSContext private structure which had AVIOContext and it was causing an issue with my alignment! I will do more testing tomorrow and come back ( This is critical and should probably update all long/unsigned long (from C) in structs with Int32/UInt32 (int/uint). Update: I've a created a sample C lib and confirmed that the size of long and unsigned long is 4 bytes for Windows x64 (imported to c# x64 with a testing struct to confirm it) |
The good news is that only AVIOContext (and _GUID?) struct is affected. I used your generator to see from the included c headers which have types long / unsigned long : -
The bad news should also check functions for in/out params with longs (wasn't able to find any) Opened also a 'wish' ticket in FFmpeg trac Possible solutions:
|
Seems I have free weekend so I'll try to retest this on linux and mac. |
@iamcarbon need an assistance here as seems if I do exclusion to this structure all works well - from all test I maid it seems structure size is no different across intel platforms, cant verify it for arm based recent macs |
it sounds funny but if have somewhere definition in preprocessor true = false I have to put more effort to to be handled |
ghosting structures might play here if don't blow library size and do cross platform tricks. Do have definitive felling it has to be reported back to ffmpeg team |
Found a solution for this, tested and works as it should... // https://github.com/dotnet/runtime/issues/13788
PrimitiveType.Long => "CLong",
PrimitiveType.ULong => "CULong", |
Looks like I have to create platform specific versions as looked in this: Boils down on windows C(U)Long is actually int. And again we can forget about .net standard 2 it is dotnet 6 change. May be forking this types from dot net core source code would be a solution. |
@Ruslan-B Found more issues (mainly for x86) with ptrdiff_t (which seems to be resolved with nint) / size_t data (which seems to be resolved with nuint). Tested with AVFrame that has crop variables as long in x86 it will have wrong values (eg duration field at latest master build). |
great here we go again, means all longs are probably ints but only on windows. I'll check how to separate platforms in nuget - the silly things is it has to be per platform expect windows all the same. |
none the less after 12 years we solving this "mystery" |
I don't think that you need to separate the nuget to platforms-OSes, using CULong / CLong and nuint / nint should be fine for both x86/x64 and for windows, linux, macos. |
No wont work - given binaries as for .net has to have different structures. I'll look into this. Easy fix for windows - however, will break any *nix system. Will figure out. Besides can be solved with shadow structures. Maybe an option. |
@Ruslan-B I'm confused, you might be too. I create a NuGet package from Windows with AVIOContext struct (with CULong) and then use it from Ubuntu. They have different size when I run sizeof(AVIOContext) and they should work fine. So:- #if TARGET_WINDOWS
using NativeType = System.UInt32;
#else
using NativeType = System.UIntPtr;
#endif The confusing part for me is when the preprocessor directives will be calculated/executed. During Pack/Compile/Build of the package library, during the compilation of the parent project or during runtime. I'm still not sure to be honest but my testing shows that is not happening during the packing of the initial library which means that you don't need to create multiple platforms-os packages. Update: FFmpeg.AutoGen/FFmpeg.AutoGen/FFmpeg.cs Line 17 in e129a31
This could be define as preprocessor directive https://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/ This is how donet actually does that for TARGET_WINDOWS https://github.com/dotnet/runtime/blob/703df3b3d7ef6c92ffdf3e9221d82539a9dd5fce/src/libraries/System.Net.Security/src/System.Net.Security.csproj#L16 So, my understanding is that all those will be calculated during the final binary which it will be OS specific (you cannot have the same binary for windows/linux/macos). This might not true only for AnyCpu but again we don't need to worry about this as far as we use the proper types (eg. nint). |
I guess I will create another package which target windows OS. we already have an abstraction, kind of clutch but transparent. |
Just updated to the latest version and using the latest build from ffmpeg and my code is throwing AccessViolationException wherever i'm calling avio_close. Are you seeing the same thing?
Not sure if this is a problem with ffmpeg or the wrapper
The text was updated successfully, but these errors were encountered: