System.Exception Error when executing code in INIT procedure(s)

This forum is meant for questions and discussions about the X# language and tools
alex_schmitt
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

System.Exception Error when executing code in INIT procedure(s)

Post by alex_schmitt »

Hi all,

I finally managed to compile my migrated VO project with 0 Errors after some weeks now :) (while I had probably 2-3k initially).
Great success so far. But when I run the project it got stuck in startup and I get the exception handling popup with this content:

Code: Select all

System.Exception
Error when executing code in INIT procedure(s)

Callstack : 
static Int32 MainApplication.Exe.Functions.Start()()   :  C:XporterOutputMainApplication__Start.prg  :  2
This also holds if I limit my __Start.prg to this code:

Code: Select all

[STAThread];
FUNCTION Start() AS INT
	LOCAL oXApp AS XApp
	TRY
		oXApp := XApp{}
		oXApp:Start()
	CATCH oException AS Exception
		ErrorDialog(oException)
	END TRY
RETURN 0

CLASS XApp INHERIT App
METHOD Start() 
  QOut("Hey")
  RETURN NIL	
END CLASS
Any idea what I am missing here?

Best,
Alex
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

System.Exception Error when executing code in INIT procedure(s)

Post by robert »

Alex,

I suspect that you have INIT procedures in your code (PROCEDURE SomeName _INIT) or in one of the libraries that you have linked in your application. These is an error in one of these INIT procedures.
The code in these init procedures is run before code in the body of your start function is running

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

System.Exception Error when executing code in INIT procedure(s)

Post by Chris »

Alex,

Just start the app from the IDE in debug mode and it should stop where the error happens. Because I think you are using XIDE,
just start the app with Shift+F5, or Debug/Start integrated debugging.


.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
alex_schmitt
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

System.Exception Error when executing code in INIT procedure(s)

Post by alex_schmitt »

Dear both,

thanks!

@Chris Shift+F5 does not help as the app crashes before it reaches the first line of code in the app.

Indeed it seems to be an INIT procedure issue related to BBrowser.
I could reproduce the error with the standard Pizza example by adding bTools.bBrowser.dll (Timestamp 23.12.2019). And then when starting the Pizza example the same error occurs. BTW this does not hold for bTools.bServer.dll.
pizza.PNG
pizza.PNG (11.45 KiB) Viewed 780 times
Now apparently I can't be the only one including that DLL so I am wondering why this does only show up in my case...
I use latest X#, so potentially a version mismatch with the DLL?
Haven't tried compiling BBrowser from scratch on my own yet - so maybe I try checking this one also.

Best,
Alex
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

System.Exception Error when executing code in INIT procedure(s)

Post by Chris »

Hi Alex,

This dll is from more than a year ago, so I'm pretty sure the problem is that it is outdated indeed. I think in the windows event log you will see a better error message, that this dll relies on a previous version of the X# runtime.

So you either need to use a later dll, or as you said recompile the project yourself. But if the dlls are outdated, I suspect that the code you have is also outdated, so you might need to download a newer version anyway.

HTH,
Chris

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
alex_schmitt
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

System.Exception Error when executing code in INIT procedure(s)

Post by alex_schmitt »

Thanks Chris, indeed. BBrowser was outdated. I received version 4.3.29 when I purchased it couple of weeks ago, but there is already 4.4.33 available. That did the trick!

Now that I am able to run my code, I am facing couple of issues.

1) when casting objects I am receiving System.InvalidCastException where I am convinced I should not get them. Here is an example that isolates the problem:

Code: Select all

CLASS classA
END CLASS

CLASS classB INHERIT classA
END CLASS

FUNCTION Start( ) AS VOID
	LOCAL oObject AS classB
                                    
	oObject:=classB{}
	
	System.Console.WriteLine(oObject IS classB)	// outputs true
	System.Console.WriteLine(oObject IS classA)	// outputs true
	oObject:=(classB)classA{} // throws an error.
	
RETURN

When executing I am getting System.InvalidCastException. What could be wrong here?

2) When there was an error in Visual Objects the Errorhandler would allow to jump into the code (e.g. via debug) and often ignore the error. The execution could mostly be continued. Now in XSharp and XIDE only option I have is to abort/cancel/ignore. But in all these cases, the app closes. No option to continue the execution. Is that due to .Net? Or am I missing a setting?

Best,
Alex
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

System.Exception Error when executing code in INIT procedure(s)

Post by Chris »

Hi Alex,

1. That's a correct error. You are trying to cast ClassA to ClassB, in other words telling the compiler that ClassA contains everything ClassB has, but this is not true. It's easier to explain by including a member of the class:


Code: Select all

CLASS classA
EXPORT AvailaboleInBothAandB AS INT

CLASS classB INHERIT classA
EXPORT OnlyAvailableInB AS INT
// it also inherits "AvailaboleInBothAandB" from the parent class

FUNCTION Start( ) AS VOID
	LOCAL a AS classA
	LOCAL b AS classB
	a:=classA{}
	b:=(classB)a
	? b:OnlyAvailableInB

It's very similar code to yours, and as you can see, the "a" var holds an instance of the classA type, which only contains the OnlyAvailableInB iVar. In the next line, we are trying to (cast and) assign this object to a var of type classB, which is supposed to have two iVars, the inherited one "AvailaboleInBothAandB", but also the one defined in that class, "OnlyAvailableInB". Clearly our object has only one of those iVars, hence the error.

What is possible to do, is to make the opposite cast:


Code: Select all

FUNCTION Start( ) AS VOID
	LOCAL a AS classA
	LOCAL b AS classB
	b:=classB{}
	a:=(classA)b
	? a:AvailaboleInBothAandB

here "b" contains an instance of the child class classB, which includes everything the parent classA has, so it's fine to cast it into classA


2. Yes, unfortunately this is extremely difficult to emulate in .Net, but actually we are working right now trying to imporve this as much as possible. If you'd like, please post some exact samples of code, so we can give you some suggestions on how to make it adjust to make it work somewhat more similar to VO.

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
alex_schmitt
Posts: 85
Joined: Wed Jan 23, 2019 7:54 pm
Location: Germany

System.Exception Error when executing code in INIT procedure(s)

Post by alex_schmitt »

Hi Chris,

Thanks for sending me these examples!

As for 1)

I am still stuck in my understanding the differences between VO and XS:

I can take your code from the first example, adapt it slightly (i.e. removing the cast) and execute it in VO a follows:

Code: Select all


CLASS classA
EXPORT AvailaboleInBothAandB AS INT

CLASS classB INHERIT classA
EXPORT OnlyAvailableInB AS INT
// it also inherits "AvailaboleInBothAandB" from the parent class

FUNCTION Start( ) AS VOID
	LOCAL a AS classA
	LOCAL b AS classB
	a:=classA{}
	b:=a
	QOut( b:OnlyAvailableInB)       

With the assignment

Code: Select all

b:=a
in VO my assumption was that this is doing an implicit cast from classA to classB. At least, that is not throwing a cast exception and would execute fine and would output "0" for b:OnlyAvailableinB.

Executing in XS the compiler would first complain that I am not using a cast ("error XS0266: Cannot implicitly convert type 'classA' to 'classB'. An explicit conversion exists (are you missing a cast?)"), and if I cast,

Code: Select all

b:=(classB)a
it would throw an invalid cast exception during execution (as you have already stated below).

In my VO code I am often declaring generic variables as here in the example and then I would instantiate subclasse and assign it to the super class variable - and it seems to be working if I am not completely wrong. So here I would apparently have to rewrite couple of places to make it run again in XS.

As for 2)
Thanks for the offer! I am still trying to get some orientation for now, but essentially I may ask for some support :)

Best,
Alex
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

System.Exception Error when executing code in INIT procedure(s)

Post by Chris »

Hi Alex,

Indeed this works in VO, but only reason why it does is because the VO compiler is very sloppy and does not find many errors in the code like this one, but I'm pretty sure such code will lead to memory corruption sooner or later. Maybe a better way to demonstrate the problem in VO is to use another sample, without inheritance. Please try this in VO:

Code: Select all

CLASS classA
EXPORT OnlyInAlpha AS INT

CLASS classB
EXPORT OnlyInBeta AS INT

FUNCTION Start() AS INT
	LOCAL a AS classA
	LOCAL b AS classB
	a := classA{}
	b := a
	a := b
	a:OnlyInAlpha := 123 
	? b:OnlyInBeta // 123!!! Where did this come from???
WAIT
RETURN 0
As you can see, we are assigning 2 completely different classes to different vars and the compiler still does not report an error. But at runtime, we assign a value to an EXPORT of one object, and then it magically appears to a different EXPORT of the other object!

The reason why this happens is that the memory layout for those two classes is the same, so the export of one object is saved in the same memory offset as the other one. If the class definitions were different, I think it's very probable that at some point a corruption would occur and it would be impossible to detect where it happened exactly. Fortunately X# catches such errors.

I hope I have explained it better, but please let us know if you need any other clarification!


.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

System.Exception Error when executing code in INIT procedure(s)

Post by Chris »

Hi Alex,

Forgot to comment on that:
alex_schmitt wrote: In my VO code I am often declaring generic variables as here in the example and then I would instantiate subclasse and assign it to the super class variable - and it seems to be working if I am not completely wrong. So here I would apparently have to rewrite couple of places to make it run again in XS.
This is perfectly valid, assigning an instance of a child class to a var declared as parent is fine, since the object is guaranteed to "contain" everything that the var can access. It's the opposite that is invalid, assigning an instance of a parent class to a var typed as the child.

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply