FFmpeg.AutoGen
FFmpeg.AutoGen copied to clipboard
Invalid generated delegate AVClass_item_name (returns string instead of byte*)
Hi @Ruslan-B,
Got an access violation with this delegate and fixed it by returning byte*. Eg:-
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public unsafe delegate byte* AVClass_item_nameX (void* @ctx); // FFmpeg.Autogen bug
public static string? GetProtocolName(AVIOContext* pb)
{
if (pb == null || pb->av_class == null || pb->av_class->child_next.Pointer == nint.Zero)
return null;
var ChildNext = (AVClass_child_next) Marshal.GetDelegateForFunctionPointer(pb->av_class->child_next.Pointer, typeof(AVClass_child_next));
var child = ChildNext(pb, null);
var cl = *(AVClass **)child;
if (cl == null || cl->item_name.Pointer == nint.Zero)
return null;
var ItemName = (AVClass_item_nameX) Marshal.GetDelegateForFunctionPointer(cl->item_name.Pointer, typeof(AVClass_item_nameX));
return Marshal.PtrToStringUTF8((nint)ItemName(child));
}
Might also worth implicit cast from all _func to delegates?
Can I ask for minimal example?
@Ruslan-B Add this function to your 1st example in VideoStreamDecoder.cs
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public unsafe delegate byte* AVClass_item_nameX (void* @ctx); // FFmpeg.Autogen bug
public static string? GetProtocolName(AVIOContext* pb)
{
if (pb == null || pb->av_class == null || pb->av_class->child_next.Pointer == IntPtr.Zero)
return null;
var ChildNext = (AVClass_child_next) Marshal.GetDelegateForFunctionPointer(pb->av_class->child_next.Pointer, typeof(AVClass_child_next));
var child = ChildNext(pb, null);
var cl = *(AVClass **)child;
if (cl == null || cl->item_name.Pointer == IntPtr.Zero)
return null;
var ItemName = (AVClass_item_name) Marshal.GetDelegateForFunctionPointer(cl->item_name.Pointer, typeof(AVClass_item_name));
return ItemName(child);
//return Marshal.PtrToStringUTF8((IntPtr)ItemName(child));
}
And add this line to test it after avformat_open_input in the constructor (line 24)
var proto = GetProtocolName(pFormatContext->pb);
You will notice that it will crash at that line with access violation exit code. Then you switch to AVClass_item_nameX instead of AVClass_item_name and use the commented return instead to get the proper result.
var ItemName = (AVClass_item_nameX) Marshal.GetDelegateForFunctionPointer(cl->item_name.Pointer, typeof(AVClass_item_nameX));
...
return Marshal.PtrToStringUTF8((IntPtr)ItemName(child));
Got it. Thanks a lot. I'll look into it.