FFmpeg.AutoGen icon indicating copy to clipboard operation
FFmpeg.AutoGen copied to clipboard

Invalid generated delegate AVClass_item_name (returns string instead of byte*)

Open SuRGeoNix opened this issue 2 years ago • 3 comments

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?

SuRGeoNix avatar May 13 '23 16:05 SuRGeoNix

Can I ask for minimal example?

Ruslan-B avatar May 22 '23 12:05 Ruslan-B

@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));

SuRGeoNix avatar May 24 '23 06:05 SuRGeoNix

Got it. Thanks a lot. I'll look into it.

Ruslan-B avatar Jul 27 '23 15:07 Ruslan-B