Self Pointer Ambiguous Context Crash(Now a Zero-Address)

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Silentdarkness12
Posts: 1555
Joined: Thu Aug 15, 2013 5:34 pm
Location: Plains of Pride

Self Pointer Ambiguous Context Crash(Now a Zero-Address)

Post by Silentdarkness12 »

I'm having a bit of trouble with making an addon for Hideous Destructor to add a shield to the arsenal.

Code: Select all

Class CGIUsable : HDPickup
{
Default
{
+COUNTITEM;
+INVENTORY.INVBAR;
  Inventory.MaxAmount 1;
  Inventory.Icon "SEPSA0";
  hdpickup.bulk 80;
  Inventory.PickupMessage "Picked up a Cover Generation Initializer.";
}
int shieldid;
int loadcell;
States
{
Spawn:
    SEPS A 1;
	SEPS A 1;
    Loop;
  Use:
  TNT1 A 1{
  let bbb=HDMagAmmo(findinventory("HDBattery"));
  if(!bbb){
  A_Log("\cd[CGI]\cj  No power. Please load 1 cell pack before deploying.",true);
  return;
  }else{
				loadcell=invoker.bbb.TakeMag(true);
			}
  }
TNT1 A 1{
	A_SpawnItemEx ("CGIActive", 56, 0, 8, 0, 0, 0, 0, SXF_NOCHECKPOSITION|SXF_TRANSFERTRANSLATION|SXF_TRANSFERPOINTERS|SXF_SETMASTER);
	actor sss; int iii;
  let SSSS=CGIActive(SSS);
  SSSS.battery=loadcell;
  SSSS.shieldid=invoker.shieldid;
	invoker.goawayanddie();
	} 
    Stop;
}
}
Getting 2 "Self Pointer is used in an ambiguous context" errors in this, on these lines:

Code: Select all

loadcell=invoker.bbb.TakeMag(true);

Code: Select all

 SSSS.battery=loadcell;
Last edited by Silentdarkness12 on Fri Jun 22, 2018 5:17 pm, edited 2 times in total.
Blue Shadow
Posts: 4949
Joined: Sun Nov 14, 2010 12:59 am

Re: Self Pointer Ambiguous Context Crash

Post by Blue Shadow »

'loadcell' is a variable of the item itself (invoker), so try changing those lines to this:

Code: Select all

invoker.loadcell=bbb.TakeMag(true);

Code: Select all

 SSSS.battery=invoker.loadcell;
User avatar
Silentdarkness12
Posts: 1555
Joined: Thu Aug 15, 2013 5:34 pm
Location: Plains of Pride

Re: Self Pointer Ambiguous Context Crash

Post by Silentdarkness12 »

Many thanks. It's good now.
User avatar
Silentdarkness12
Posts: 1555
Joined: Thu Aug 15, 2013 5:34 pm
Location: Plains of Pride

Self Pointer Ambiguous Context Crash

Post by Silentdarkness12 »

Actually, wait, no. It's not. New game and it crashes. Write to Address Zero crash.

Code: Select all

VM execution aborted: tried to write to address zero.
Called from CGIUsable.StateFunction.3 at HDShield.pk3:zscript, line 722
Called from state CGIUsable.3 in inventory state chain in CGIUsable
Called from CustomInventory.CallStateChain [Native]
Called from CustomInventory.Use at gzdoom.pk3:zscript/inventory/stateprovider.txt, line 95

Code: Select all

SSSS.battery=invoker.loadcell;
User avatar
phantombeta
Posts: 2088
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Self Pointer Ambiguous Context Crash(Now a Zero-Address)

Post by phantombeta »

Code: Select all

A_SpawnItemEx ("CGIActive", 56, 0, 8, 0, 0, 0, 0, SXF_NOCHECKPOSITION|SXF_TRANSFERTRANSLATION|SXF_TRANSFERPOINTERS|SXF_SETMASTER);
   actor sss; int iii;
You never set sss, so it's always null. Now, what I think you wanted to do here was

Code: Select all

bool spawned; actor sss;
[spawned, sss] = A_SpawnItemEx ("CGIActive", 56, 0, 8, 0, 0, 0, 0, SXF_NOCHECKPOSITION|SXF_TRANSFERTRANSLATION|SXF_TRANSFERPOINTERS|SXF_SETMASTER);
After this, you have to check if spawned is true and sss isn't null.
Yes, you need to check both. For some reason it doesn't return null if it failed to spawn.

Also, you really should use descriptive variable names and proper indentation. Bad indentation and variable names means it's harder for others to help, specially when the indentation is really bad, or even non-existent.
As an example, I wanted to try to help fix the MP issues in HD, but with the bad indentation and unclear, confusing variable names, I gave up due to how unreadable HD's source code is.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Self Pointer Ambiguous Context Crash(Now a Zero-Address)

Post by Graf Zahl »

phantombeta wrote: Yes, you need to check both. For some reason it doesn't return null if it failed to spawn.
That "some reason" is one special situation: Imagine you spawned something that immediately got destroyed, but still need to perform some action with that actor. In that case you both need the pointer and a success value. One of the internal weapons needs this, I can't remember right now which one.
User avatar
phantombeta
Posts: 2088
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Self Pointer Ambiguous Context Crash(Now a Zero-Address)

Post by phantombeta »

I should probably have specified - the pointer is non-null, but it's already freed by the time A_SpawnItemEx returns. Accessing the pointer crashes the game with a fatal error.
I'd make a report for it, but my computer's dead, so I can't really do that.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Self Pointer Ambiguous Context Crash(Now a Zero-Address)

Post by Graf Zahl »

It cannot be freed. The garbage collector shouldn't kick in at that point, it only runs after a frame has ended. If it crashes on the pointer it must be bogus.
Post Reply

Return to “Scripting”