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

-net-lib fails to read C# class that implements finalizer with "Invalid override" error #5241

Closed
vroad opened this issue May 16, 2016 · 24 comments
Closed
Assignees
Labels
Milestone

Comments

@vroad
Copy link
Contributor

@vroad vroad commented May 16, 2016

C# compiler seems to be thinking that this assembly is invalid. I'm currently using external class to avoid this issue, but it would be great if I can use any assemblies directly.

Export/windows/cs/bin/cs-ndll.dll
@ cs.NDLLFunction (Finalize):1: character 0 :
Invalid override on field 'Finalize': class has no super class
Export/windows/cs/bin/cs-ndll.dll @ cs.NDLLFunction:1: character 0 : Defined in
this class

@vroad
Copy link
Contributor Author

@vroad vroad commented May 16, 2016

Sorry I was wrong. Haxe fails to read any class that implements finalizer. In this case, ~NDLLFunction().

@vroad vroad changed the title -net-lib fails to read C# class that implements IDisposable interface with "Invalid override" error -net-lib fails to read C# class that implements finalizer with "Invalid override" error May 16, 2016
@nadako
Copy link
Member

@nadako nadako commented May 18, 2016

I think I also encountered this at some moment, @waneck could you look into that? I don't think that it's a regression, but it's still a bug that looks severe and annoying, tho I feel like it's not that hard to fix. :)

@vroad could you maybe provide a dll that fails like that?

@nadako nadako added this to the 3.3.0-rc1 milestone May 21, 2016
@nadako
Copy link
Member

@nadako nadako commented May 21, 2016

I'm gonna set this to 3.3 and hope @waneck will be able to fix this during his next haxe session.

waneck added a commit that referenced this issue May 26, 2016
@waneck
Copy link
Member

@waneck waneck commented May 26, 2016

@vroad can you please send a test case that fails? I've just added a test using finalizers which succeeds in C#

@waneck waneck closed this May 26, 2016
@vroad
Copy link
Contributor Author

@vroad vroad commented May 27, 2016

@waneck Sure.

  1. Compile cs-ndll from source (https://github.com/vroad/cs-ndll)
  2. Write some Haxe program that refers to this .NET assembly
  3. Compile with haxe -cs obj Main.hx -net-lib cs-ndll.dll

Will cause this error:

cs-ndll.dll @ cs.NDLLFunction (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
cs-ndll.dll @ cs.NDLLFunction:1: character 0 : Defined in this class
import cs.NDLLFunction;

class Main
{
    public static function main()
    {
        trace("test");
    }
}
@vroad
Copy link
Contributor Author

@vroad vroad commented May 27, 2016

I avoided this issue by moving cs-ndll to lime template. This library is now compiled as part of C# application .Not only because of this bug though. I need to have access to type information which doesn't seems to be accessible with Reflection API.

@waneck
Copy link
Member

@waneck waneck commented May 27, 2016

Thank you. As for the Reflection API, you can still use C#'s reflection api on Haxe - e.g. cs.system.reflection.* package

@waneck waneck reopened this May 27, 2016
@vroad
Copy link
Contributor Author

@vroad vroad commented May 27, 2016

My last comment about Reflection was bit misleading. I was not talking about Haxe Reflection issue.

Since cs-ndll was seperate project at that time (and was written in C#), I was wondering if it's possible to access to length/__a of an Haxe Array without referencing it at compile time. It seems impossible to do this without obtaining FieldInfo every time because Haxe Array is a generic class. And obtaining fields with C# Reflection should be slower than directly using them. So I decided to include this in lime repository instead.

@Simn Simn modified the milestones: 3.3.0-rc1, 3.3.0 Jun 2, 2016
@Simn Simn added platform-cs and removed platform-cs labels Jul 18, 2016
@waneck
Copy link
Member

@waneck waneck commented Oct 8, 2016

@vroad I'm trying to test this, but I cannot compile cs-ndll . The sln seems to be missing a csproject. Can you send me the compiled dll? Thanks!

@vroad
Copy link
Contributor Author

@vroad vroad commented Oct 9, 2016

@frabbit
Copy link
Member

@frabbit frabbit commented Nov 24, 2016

@waneck is this relevant for 3.3 or should we postpone this issue?

@Simn Simn modified the milestones: 3.3.0, 3.4 Dec 10, 2016
@Simn Simn removed this from the 3.4 milestone Jan 9, 2017
@Simn Simn modified the milestones: 4.0, 3.4 Jan 9, 2017
@ianharrigan
Copy link

@ianharrigan ianharrigan commented Feb 23, 2017

Im getting something similar when trying to use winforms in haxe 3.2.1, is it something related? I must admit i know very little about hxcs and c# in general, so its very possible it something im not doing correctly.

Cheers,
Ian

@mrcdk
Copy link
Contributor

@mrcdk mrcdk commented Oct 1, 2017

This is still a problem in Haxe 4.0.preview.1

Godot is going to merge C# support pretty soon and when I tried using Haxe the same problem happens:

../.mono/assemblies/GodotSharp.dll @ godot.Object (Finalize):1: character 1 : Invalid override on field 'Finalize': class has no super class
../.mono/assemblies/GodotSharp.dll @ godot.Object:1: character 1 : Defined in this class
../.mono/assemblies/GodotSharp.dll @ godot.RID (Finalize):1: character 1 : Invalid override on field 'Finalize': class has no super class
../.mono/assemblies/GodotSharp.dll @ godot.RID:1: character 1 : Defined in this class
../.mono/assemblies/GodotSharp.dll @ godot.NodePath (Finalize):1: character 1 : Invalid override on field 'Finalize': class has no super class
../.mono/assemblies/GodotSharp.dll @ godot.NodePath:1: character 1 : Defined in this class

😢

@dmitryhryppa
Copy link

@dmitryhryppa dmitryhryppa commented Oct 1, 2017

The same in Haxe 3.4 :(

C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.ImageList (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.ImageList:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.ToolStripPanel (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.ToolStripPanel:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Drawing.dll @ cs.system.drawing.imaging.EncoderParameter (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Drawing.dll @ cs.system.drawing.imaging.EncoderParameter:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Drawing.dll @ cs.system.drawing.imaging.ImageAttributes (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Drawing.dll @ cs.system.drawing.imaging.ImageAttributes:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Drawing.dll @ cs.system.drawing.text.FontCollection (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Drawing.dll @ cs.system.drawing.text.FontCollection:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.Cursor (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.Cursor:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.NativeMethods (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.NativeMethods:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.UnsafeNativeMethods (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.UnsafeNativeMethods:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.Control (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.Control:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.Control (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.Control:1: character 0 : Defined in this class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.ApplicationContext (Finalize):1: character 0 : Invalid override on field 'Finalize': class has no super class
C:/Windows/Microsoft.NET/Framework/v4.0.30319/System.Windows.Forms.dll @ cs.system.windows.forms.ApplicationContext:1: character 0 : Defined in this class
@mrcdk
Copy link
Contributor

@mrcdk mrcdk commented Oct 1, 2017

Did some digging and this may be the issue.

A simple C# program:

using System;
public class Program {
	public static void Main() {
		Console.WriteLine("Hello World");
	}

	~Program() { }
}

Will have this IL for the destructor/Finalize:

  .method family hidebysig virtual instance void 
          Finalize() cil managed
  {
    // 
    .maxstack  1
    .try
    {
      IL_0000:  nop
      IL_0001:  nop
      IL_0002:  leave.s    IL_000c

    }  // end .try
    finally
    {
      IL_0004:  ldarg.0
      IL_0005:  call       instance void [mscorlib]System.Object::Finalize()
      IL_000a:  nop
      IL_000b:  endfinally
    }  // end handler
    IL_000c:  nop
    IL_000d:  ret
  } // end of method Program::Finalize

But the sample project that @vroad posted has this IL for the destructor/Finalize:

.method family hidebysig virtual 
	instance void Finalize () cil managed 
{
	.override method instance void [mscorlib]System.Object::Finalize()
	// Method begins at RVA 0x3790
	// Code size 17 (0x11)
	.maxstack 2

	.try
	{
		IL_0000: ldarg.0
		IL_0001: ldc.i4.0
		IL_0002: call instance void cs.NDLLFunction::Dispose(bool)
		IL_0007: leave.s IL_0010
	} // end .try
	finally
	{
		IL_0009: ldarg.0
		IL_000a: call instance void [mscorlib]System.Object::Finalize()
		IL_000f: endfinally
	} // end handler

	IL_0010: ret
} // end of method NDLLFunction::Finalize

Which is similar to the Godot's one:

.method family hidebysig virtual 
	instance void Finalize () cil managed 
{
	.override method instance void [mscorlib]System.Object::Finalize()
	// Method begins at RVA 0x2ab18
	// Code size 17 (0x11)
	.maxstack 2

	.try
	{
		IL_0000: ldarg.0
		IL_0001: ldc.i4.0
		IL_0002: callvirt instance void Godot.Object::Dispose(bool)
		IL_0007: leave.s IL_0010
	} // end .try
	finally
	{
		IL_0009: ldarg.0
		IL_000a: call instance void [mscorlib]System.Object::Finalize()
		IL_000f: endfinally
	} // end handler

	IL_0010: ret
} // end of method Object::Finalize

Notice the .override method instance void [mscorlib]System.Object::Finalize() which is what I think is causing the problem.

I have no idea how to solve this but maybe this is more useful for you @waneck

@nadako
Copy link
Member

@nadako nadako commented Oct 1, 2017

I'm trying to look into this, but it would really help to have a minimal C# source file that provides the failing assembly.

@nadako
Copy link
Member

@nadako nadako commented Oct 1, 2017

I tried decompiling the cs-ndll.dll and compiling it back with ms .net and that doesn't seem to be broken :-/

nadako added a commit that referenced this issue Oct 1, 2017
@nadako
Copy link
Member

@nadako nadako commented Oct 1, 2017

Anyway, I made the IL loader just skip Finalize methods for now, because they are so special (if I understand correctly, that's the actual name for the destructor, right?). The cs-ndll.dll seems to load fine now, could you guys test it a bit?

@nadako
Copy link
Member

@nadako nadako commented Oct 1, 2017

@waneck please check the referenced commit when you have time.

@mrcdk
Copy link
Contributor

@mrcdk mrcdk commented Oct 1, 2017

Weird... I've got the simple program IL from here https://dotnetfiddle.net/pTR3SJ (the last dropdown from the center let you see the IL code)

But I've created this project with Xamarin Studio:
test3.zip

And the Finalize IL has an override like the cs-ndll and godot which, of couse, breaks haxe with the same issue:

test3.dll @ test3.MyClass (Finalize):1: character 1 : Invalid override on field 'Finalize': class has no sup
er class
test3.dll @ test3.MyClass:1: character 1 : Defined in this class
@mrcdk
Copy link
Contributor

@mrcdk mrcdk commented Oct 1, 2017

Yes, it works @nadako ! Now to find how to make it work with Godot 😄

@nadako
Copy link
Member

@nadako nadako commented Oct 2, 2017

Maybe we should include this fix in 3.4.4, will it be a thing @Simn ?

@Simn
Copy link
Member

@Simn Simn commented Oct 2, 2017

Yah, someone just has to start a branch...

@Simn Simn modified the milestones: Release 4.0, Bugs Apr 17, 2018
@kLabz
Copy link
Contributor

@kLabz kLabz commented Jun 14, 2019

This seems to have been fixed in haxe 4 preview 2:

$ haxe -version && haxe-cs-test 5241
4.0.0 (git build master @ 2344f23) # preview 1
Running haxe projects/Issue5241/compile.hxml
./cs-ndll.dll @ cs.NDLLFunction (Finalize):1: character 1 : Invalid override on field 'Finalize': class has no super class
./cs-ndll.dll @ cs.NDLLFunction:1: character 1 : Defined in this class
Done running 1 tests with 1 failures

$ haxe -version && haxe-cs-test 5241
4.0.0 (git build development @ a018cbd) # preview 2
Running haxe projects/Issue5241/compile.hxml
haxelib run hxcs hxcs_build.txt --haxe-version 4000 --feature-level 1
Note: dmcs is deprecated, please use mcs instead!

Done running 1 tests with 0 failures
@kLabz kLabz closed this Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants
You can’t perform that action at this time.