Crash when calling objDestroy in Mod

These are old bug reports that have been closed.
Locked
User avatar
Mutos
Militia Lieutenant
Militia Lieutenant
Posts: 218
Joined: Thu Aug 14, 2008 3:31 am
Location: Near Paris, France
Contact:

Hi George, hi all,


I now report a crash cause that occurs on my Stars of Call Adventure Mod and bugged me for some time before I was finally able to pinpoint it.

I still don't know if it is simply a misuse of the objDestroy function or a bug with save/load system and space objects. Surely you could help me on it, George.

Here are the facts !

First, everything happens in the following function so it could be useful to know how it is made :

Code: Select all

<!--
	socOnTimerSetNextWaypoint
		When ?
			Called by OnTimerSetNextWaypoint Event
		What ?
			Update ship status
			Choose a random waypoint destination
-->
(setq socOnTimerSetNextWaypoint
	(lambda (sourceShip)
		<!-- Function code itself : begin -->
		(block (message oldStatus newStatus nextWaypoint oldWaypoint angle distance)
			; Log beginning of function
			(setq message (cat "(" (unvGetTick) ") Ship " (objGetName sourceShip) " #" sourceShip " : Entering socOnTimerSetNextWaypoint"))
			(dbglog "")
			(dbglog message)

			; Set variables
			(setq oldStatus (objGetData sourceShip "status"))
			(setq newStatus "statusToWaypoint")
			(setq oldWaypoint (objGetData sourceShip "waypoint"))

			; Destroy older waypoint if it exists
			(objDestroy oldWaypoint)

			; Find next waypoint
			(setq nextWaypoint
				(sysCreateMarker
					""
					(sysVectorPolarOffset (objGetPos sourceShip) (random 0 359) (random 400 800))
					(objGetSovereign sourceShip)
				)
			)

			; Logging new waypoint
			(setq angle (sysVectorAngle (objGetPos nextWaypoint)))
			(setq distance (sysVectorDistance Nil (objGetPos nextWaypoint)))
			(setq message (cat "  Setting new waypoint [" angle "°;" distance "ls]"))
			(dbglog message)

			; Store new destination into ship data
			(objSetData sourceShip "waypoint" nextWaypoint)

			; Remove any attacker from the ship's data
			; and cancel the associated event
			(objSetData sourceShip "attacker" Nil)
			(sysCancelTimerEvent sourceShip "OnCheckEndStrikeBack")

			; Set new orders
			(shpCancelOrders sourceShip)
			(shpOrderGoto sourceShip nextWaypoint)

			; Set new status
			(objSetData sourceShip "status" newStatus)

			; Log end of function
			(setq message (cat "(" (unvGetTick) ") Ship " (objGetName sourceShip) " #" sourceShip " : Exiting socOnTimerSetNextWaypoint"))
			(dbglog message)
		)
		<!-- Function code itself : end -->
	)
)
<!-- Function end -->
This function instructs a ship to go to a random waypoint. First it destroys any pre-existing waypoint. Then it creates a random waypoint and stores its ID on the ship so it can be destroyed when the waypoint has been reached and the function is called again. I already know that objDestroy called on Nil just does nothing, that's why I don't test anything before calling it.

Crashes occur when I save a game then restore it. They occur on the (objDestroy oldWaypoint) line. They seem not to occur before saving the game, but this point is still subject to further testing. By reading on, you'll learn it can change the possible causes I'm looking for...

I debugged by logging all variables line by line. So I know that, when it crashes, it's on the objDestroy line and that the oldWaypoint passed to objDestroy has a number value. What lies behind this value I don't know...

So to the main question now... When you save a game, does the saved game include Markers ? It should, but that seems a possible scenario : you create a Marker object, you save, then you restore, the Marker hasn't been saved so when you refer to its ID, the game crashes.

I still have to thoroughly check that the function actually works OK before saving/reloading. If it works, then the crash is related to saving/reloading. If it crashes, then it would rather be related to using objDestoy on a Marker object. I'll check and let you know.

Thanks in advance for any hint that could help me to check my code more accurately !
Last edited by Mutos on Thu Jan 22, 2009 10:27 am, edited 1 time in total.
@+

Benoît 'Mutos' ROBIN
Hoshikaze 2250 Project
User avatar
Mutos
Militia Lieutenant
Militia Lieutenant
Posts: 218
Joined: Thu Aug 14, 2008 3:31 am
Location: Near Paris, France
Contact:

I have tested and logged until I found the perfect example of the crash issue. I did the following test :
1/ Set debug logs to write information about old and new waypoints,
2/ Launch a new Adventure and wait until waypoint changes appear in Debug.log, to show the function works before saving,
3/ Save the game and exit Transcendence,
4/ Relaunch Transcendence, load the game and wait for the crash,
5/ Extract the relevant parts of the Debug.log file.

I made several test runs. Finally, the crash occured immediately after the reload. When checking the log, I saw that the event that crashed was at the very first tick. This sheer luck allowed me to identify in the first run the ship and marker that caused the crash. So I post these extracts.

First, here is the code used to output these logs. I made minor changes to the code posted in the previous post, to better log what happens. You see that this time, the games won't crash on objDestroy, but on objGetPos.

Code: Select all

; Set variables
(setq oldStatus (objGetData sourceShip "status"))
(setq newStatus "statusToWaypoint")
(setq oldWaypoint (objGetData sourceShip "waypoint"))

; Logging old waypoint
(if (eq oldWaypoint Nil)
	(block Nil
		(setq message "  Old waypoint : Nil")
		(dbglog message)
	)
	(block Nil
		(setq message (cat "  Old waypoint #" oldWaypoint))
		(dbglog message)
		(setq angle (sysVectorAngle (objGetPos oldWaypoint)))
		(setq distance (sysVectorDistance Nil (objGetPos oldWaypoint)))
		(setq message (cat "  Old waypoint #" oldWaypoint " at [" angle "°;" distance "ls]"))
		(dbglog message)
	)
)

; Destroy older waypoint if it exists
(objDestroy oldWaypoint)
Then here's the log, which shows the following sequence of events :
- Tick 2 : 1st execution of socOnTimerSetNextWaypoint function : no marker exists, one is created,
- Tick 1100 : 2nd execution of socOnTimerSetNextWaypoint function : existing marker is destroyed, a new is created and takes the same ID that was just freed,
- Tick 2140 : OnOrderCompleted event is fired and schedules execution of socOnTimerSetNextWaypoint for the next tick,
- Game is then saved and restored; by sheer luck, this happens to occur just between these ticks 2140 and 2141,
- Tick 2141 : 3rd execution of socOnTimerSetNextWaypoint function : saved/restored marker crashes the game whenever used in a function.

We also see that both ship and marker take different IDs when saved/reloaded. From that we can infer that they are dynamically allocated pointers. The crash could then be caused by an invalid pointer going to a bad memory zone, or something like that.

Code: Select all

01/22/2009 08:32:51	--------------------------------------------------------------------------------
01/22/2009 08:32:51	Start logging session
01/22/2009 08:32:51	Transcendence 0.99c
01/22/2009 08:32:54	Loading extension: Extensions\ATA-GateGlobalsD8
01/22/2009 08:32:54	Loading extension: Extensions\ATA-StarSystemsD6
01/22/2009 08:32:54	Loading extension: Extensions\DebugCode
01/22/2009 08:32:54	Loading adventure desc: Extensions\HK-001-Adventure-SoC-045-Common-Functions
01/22/2009 08:32:54	Loading extension: Extensions\HK-101-Systems-CoreLeague-034-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-201-Stations-Common-019-Fix-Orbit
01/22/2009 08:32:54	Loading extension: Extensions\HK-202-Stations-CoreLeague-032-ZIP-006
01/22/2009 08:32:54	Loading extension: Extensions\HK-203-Stations-TMS-011-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-301-Ships-SystemWideFreighters-031-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-302-Ships-Pirates-021-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-303-Ships-InterStellarFreighters-014-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-304-Ships-PolicePatrol-016-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-401-Items-Shields-006-ZIP-006
01/22/2009 08:32:54	Loading extension: Extensions\HK-402-Items-Weapons-010-ZIP-006
01/22/2009 08:32:54	Loading extension: Extensions\HK-601-DockScreens-003-ZIP-006
01/22/2009 08:32:54	Loading extension: Extensions\HK-E01-IA-Ships-008-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-E02-IA-Systems-008-Common-Functions
01/22/2009 08:32:54	Loading extension: Extensions\HK-E03-IA-SystemWideFreighters-008-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-E04-IA-Pirates-008-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-E05-IA-InterStellarFreighters-008-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-E06-IA-PolicePatrol-013-Debug-Code
01/22/2009 08:32:54	Loading extension: Extensions\HK-E07-IA-PlayerShips-001-Common-Functions
01/22/2009 08:32:54	Loading extension: Extensions\HK-EFF-Debug-Facility-001-Debug-Code
01/22/2009 08:32:59	Loading adventure: Extensions\HK-001-Adventure-SoC-045-Common-Functions
01/22/2009 08:33:01
...
01/22/2009 08:33:02	(2) Ship Santa Cruz #221466632 : Entering socOnTimerSetNextWaypoint
01/22/2009 08:33:02	  Old waypoint : Nil
01/22/2009 08:33:02	  New waypoint #221503432 at [98°;1913ls]
01/22/2009 08:33:02	(2) Ship Santa Cruz #221466632 : Exiting socOnTimerSetNextWaypoint
01/22/2009 08:33:02
...
01/22/2009 08:33:26	(1100) Ship Santa Cruz #221466632 : Entering socOnTimerSetNextWaypoint
01/22/2009 08:33:26	  Old waypoint #221503432
01/22/2009 08:33:26	  Old waypoint #221503432 at [98°;1913ls]
01/22/2009 08:33:26	  New waypoint #221503432 at [84°;2424ls]
01/22/2009 08:33:26	(1100) Ship Santa Cruz #221466632 : Exiting socOnTimerSetNextWaypoint
...
01/22/2009 08:33:59
01/22/2009 08:33:59	(2140) Ship Santa Cruz #221466632 : Entering socPolicePatrolShipsOnOrdersCompleted
01/22/2009 08:33:59	  Old status : statusToWaypoint
01/22/2009 08:33:59	(2140) Ship Santa Cruz #221466632 : Exiting socPolicePatrolShipsOnOrdersCompleted
01/22/2009 08:34:03	End logging session
01/22/2009 08:35:39	--------------------------------------------------------------------------------
01/22/2009 08:35:39	Start logging session
01/22/2009 08:35:39	Transcendence 0.99c
01/22/2009 08:35:42	Loading extension: Extensions\ATA-GateGlobalsD8
01/22/2009 08:35:42	Loading extension: Extensions\ATA-StarSystemsD6
01/22/2009 08:35:42	Loading extension: Extensions\DebugCode
01/22/2009 08:35:42	Loading adventure desc: Extensions\HK-001-Adventure-SoC-045-Common-Functions
01/22/2009 08:35:42	Loading extension: Extensions\HK-101-Systems-CoreLeague-034-Debug-Code
01/22/2009 08:35:42	Loading extension: Extensions\HK-201-Stations-Common-019-Fix-Orbit
01/22/2009 08:35:42	Loading extension: Extensions\HK-202-Stations-CoreLeague-032-ZIP-006
01/22/2009 08:35:42	Loading extension: Extensions\HK-203-Stations-TMS-011-Debug-Code
01/22/2009 08:35:42	Loading extension: Extensions\HK-301-Ships-SystemWideFreighters-031-Debug-Code
01/22/2009 08:35:42	Loading extension: Extensions\HK-302-Ships-Pirates-021-Debug-Code
01/22/2009 08:35:42	Loading extension: Extensions\HK-303-Ships-InterStellarFreighters-014-Debug-Code
01/22/2009 08:35:42	Loading extension: Extensions\HK-304-Ships-PolicePatrol-016-Debug-Code
01/22/2009 08:35:42	Loading extension: Extensions\HK-401-Items-Shields-006-ZIP-006
01/22/2009 08:35:43	Loading extension: Extensions\HK-402-Items-Weapons-010-ZIP-006
01/22/2009 08:35:43	Loading extension: Extensions\HK-601-DockScreens-003-ZIP-006
01/22/2009 08:35:43	Loading extension: Extensions\HK-E01-IA-Ships-008-Debug-Code
01/22/2009 08:35:43	Loading extension: Extensions\HK-E02-IA-Systems-008-Common-Functions
01/22/2009 08:35:43	Loading extension: Extensions\HK-E03-IA-SystemWideFreighters-008-Debug-Code
01/22/2009 08:35:43	Loading extension: Extensions\HK-E04-IA-Pirates-008-Debug-Code
01/22/2009 08:35:43	Loading extension: Extensions\HK-E05-IA-InterStellarFreighters-008-Debug-Code
01/22/2009 08:35:43	Loading extension: Extensions\HK-E06-IA-PolicePatrol-013-Debug-Code
01/22/2009 08:35:43	Loading extension: Extensions\HK-E07-IA-PlayerShips-001-Common-Functions
01/22/2009 08:35:43	Loading extension: Extensions\HK-EFF-Debug-Facility-001-Debug-Code
01/22/2009 08:35:46	Loading adventure: Extensions\HK-001-Adventure-SoC-045-Common-Functions
01/22/2009 08:35:47
01/22/2009 08:35:47	(2141) Ship Santa Cruz #213081416 : Entering socOnTimerSetNextWaypoint
01/22/2009 08:35:47	  Old waypoint #221503432
@+

Benoît 'Mutos' ROBIN
Hoshikaze 2250 Project
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Mutos,
I think that your problem is in

Code: Select all

(objGetData sourceShip "waypoint")
are you storing a spaceObject with ObjSetData ?

You have to ask Betel, because he wrote on xelerus:
Very helpful function in storing data on a space object. Be careful on not storing any objects or pointers or lists containing them.
From what I can understand to store a spaceObjet inside another spaceObject you have to use objSetObjRefData

Hope I've been helpful :)
User avatar
Mutos
Militia Lieutenant
Militia Lieutenant
Posts: 218
Joined: Thu Aug 14, 2008 3:31 am
Location: Near Paris, France
Contact:

Hi digdug,


OK, I didn't notice that. Thanks a lot for the advice !

In fact all my code, test and posts were for such a trivial error ! I'll test that solution asap and let you know.
@+

Benoît 'Mutos' ROBIN
Hoshikaze 2250 Project
User avatar
Mutos
Militia Lieutenant
Militia Lieutenant
Posts: 218
Joined: Thu Aug 14, 2008 3:31 am
Location: Near Paris, France
Contact:

Hi digdug, hi all,


Tested, it works. Now I got other references to convert in my code.
@+

Benoît 'Mutos' ROBIN
Hoshikaze 2250 Project
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

I'm glad that I've been useful :)

Your adventure extension mod is really cool! :D
User avatar
alterecco
Fleet Officer
Fleet Officer
Posts: 1658
Joined: Wed Jan 14, 2009 3:08 am
Location: Previously enslaved by the Iocrym

That was a really good catch DigDug. I was staring at it, and couldn't see it...

Also Mutos, you might want to take a look here, http://www.neurohack.com/transcendence/ ... php?t=1835. In the last post F50 has a pair of functions that helps you to store lists of spaceObjects on other objects. Very useful!
User avatar
Mutos
Militia Lieutenant
Militia Lieutenant
Posts: 218
Joined: Thu Aug 14, 2008 3:31 am
Location: Near Paris, France
Contact:

Hi all,


Thanks all of you for the help ! It was just in front of me and I couldn't see it ! What a shame ! I'm ashamed !

As for the Adventure Mod, it's just the beginning and I don't find it very impressive, it lacks so many features ^-^ But I'll keep on and it'll expand...

Thanks alterecco for the helper functions, I'll give them a look and surely use them somewhere ! I'll soon need lists of many things.
@+

Benoît 'Mutos' ROBIN
Hoshikaze 2250 Project
Locked