Quantcast

Jump to content

» «
Photo

AccessViolation at Scripting.SetCarEngineOn

4 replies to this topic
Crosire
  • Crosire

    Player Hater

  • Members
  • Joined: 08 Jul 2012

#1

Posted 16 March 2013 - 09:55 PM

I'm an experienced C# coder and decided to put something up using the DotNet Scripthook. Well, everything is working quite well, except one issue I keep running into.
One of my scripting threads loops through all vehicles around the player and eventually sets the engine health to "0f" for some of them. That normally works totally fine and there isn't really anything in the code that might fail there. But since several days I keep getting a "AccessViolationException", which then obviously crashes the script. Last time that error stayed for two days and went away after several attemps of changing code, clearing settings, etc. Everything was working for some time now, but suddenly, without any change it the code it came back.

Full stack trace:

CODE
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
           at NativeInvoke.Invoke<int,int,bool,bool>(SByte* name, Int32 p1, Boolean p2, Boolean p3)
           at Scripting.?A0x8c04474b.SetCarEngineOn(Int32 , Boolean , Boolean )
           at GTA.Vehicle.set_EngineHealth(Single value)
           at --.--.Tick_Main(Object sender, EventArgs e)
           at System.EventHandler.Invoke(Object sender, EventArgs e)
           at GTA.Script.TryTick()
           at GTA.Script.DoTick()
           at GTA.ScriptThread.OnTick()


As you can see it tries to enable the car engine somewhy (while I'm only changing the engine health). I asume this is the way, the DotNet script hook changes the health there after looking into its managed source.
I'm somewhere lost here and I hope somebody with experience can help me here.

Cheers,
Crosire

Crosire
  • Crosire

    Player Hater

  • Members
  • Joined: 08 Jul 2012

#2

Posted 17 March 2013 - 03:27 PM

The error vanished after some changes again and was replaced by a new one. I'm checking all cars with "if (!vehicle.isRequiredforMission) { .. }" before I continue in my code. That works fine most of the time, but after about 2 minutes the script crashes again with the same exception:
CODE

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
           at NativeInvoke.Invoke<bool,int>(SByte* name, Int32 p1)
           at Scripting.?A0x8c04474b.IsCarAMissionCar(Int32 )
           at GTA.Vehicle.get_isRequiredForMission()
           at --.--.Tick--(Object sender, EventArgs e)
           at System.EventHandler.Invoke(Object sender, EventArgs e)
           at GTA.Script.TryTick()
           at GTA.Script.DoTick()
           at GTA.ScriptThread.OnTick()

pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#3

Posted 17 March 2013 - 05:16 PM

Do you check if the vehicle is still loaded ? You should get a no instance exception in that case but, oh well, who knows.

And try out the native functions for that to see if there is any difference.

There is a SET_ENGINE_HEALTH, there is no documentation for it, but from my experience it should have 2 parameters,

SET_ENGINE_HEALTH(Vehicle handle, Single health)

Also I have found it easier to simply have a second script doing background work, instead of a different thread on the same script. It is a bit non-sense but, for me it has been more easier to deal with. And since both scripts run from the same assembly that is super easy to transfer data between them. Just use internal static members for that, and make sure to clear them after reading, I think the Garbage Collector will not do that for you, in this case, but you can always check that.

Crosire
  • Crosire

    Player Hater

  • Members
  • Joined: 08 Jul 2012

#4

Posted 18 March 2013 - 06:04 PM

I'm already doing that by now. Having one script/thread (the scripthook handles each script in a different thread) cycling through peds and one through vehicles. So if one crashes, the other one still runs.

I will check if the errors do go away if I use native functions instead of the ones the .net/cpp scripthook provide. Thanks for the suggestion!

Crosire
  • Crosire

    Player Hater

  • Members
  • Joined: 08 Jul 2012

#5

Posted 18 March 2013 - 09:01 PM

I fixed the last error by using the native "IS_CAR_A_MISSION_CAR" now. There are still some random access violation exceptions showing up, but they don't crash the script anymore, so one step in the right direction. Thank you again.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users