Jump to content

ent->**parent** was nullptr.


Recommended Posts

Unhandled exception thrown: read access violation.
ent->**parent** was nullptr.
 

I could use some help. I have been trying to get my deletion function to work correctly, but no matter what I seem to do it always gives me a "was nullptr" error.  Everywhere I have looked for information about nullptr has not really given an explanation that I actually understand. My understanding is that a "was nullptr" error is given when you try to dereference a pointer/node, but I could never find a way to handle the issue that made sense to me. Any help is really appreciated. My code is:



void Weapon_HookFree(gentity_t *ent)
{
	ent->parent->client->fireHeld = qfalse;
	ent->parent->client->hookhasbeenfired = qfalse;
	ent->parent->client->hook = NULL;
	ent->parent->client->ps.pm_flags &= ~PMF_GRAPPLE_PULL;
	G_FreeEntity(ent);
}

This is for singleplayer only and does not show this error in mp.

gerbilOFdoom likes this
Link to comment

No way to tell without seeing all of your related code changes (specifically where Weapon_HookFree is being called from).

There's an assumption that a hook belongs to another entity (player), but that doesn't seem to be true; ent->parent should have been set long before Weapon_HookFree was called (e.g. upon hook creation) but wasn't, or it was cleared at some other point in time (or you're calling Weapon_HookFree on a non-hook entity).

 

A more general solution: learn to debug, get a backtrace, etc.

Link to comment

No way to tell without seeing all of your related code changes (specifically where Weapon_HookFree is being called from).

There's an assumption that a hook belongs to another entity (player), but that doesn't seem to be true; ent->parent should have been set long before Weapon_HookFree was called (e.g. upon hook creation) but wasn't, or it was cleared at some other point in time (or you're calling Weapon_HookFree on a non-hook entity).

 

A more general solution: learn to debug, get a backtrace, etc.

Thank you for responding to this post, The Weapon_HookFree is being called in g_active.cpp in void ClientThink_real( gentity_t *ent, usercmd_t *ucmd ).

 (ent->client->ps.weapon == WP_MELEE)
	{
		if (ucmd->buttons & BUTTON_GRAPPLE &&
			ent->client->ps.pm_type != PM_DEAD &&
			!ent->client->hookhasbeenfired &&
			!(PM_SaberInAttack(ent->client->ps.saberMove)))
		{
			if (ent->client && ent->client->hookDebounceTime > level.time)
			{
				if (client->hook)
				{
					Weapon_HookFree(client->hook);
				}
			}
			else
			{
				Weapon_GrapplingHook_Fire(ent);
				ent->client->hookhasbeenfired = qtrue;
				NPC_SetAnim(ent, SETANIM_BOTH, BOTH_PULL_IMPALE_STAB, SETANIM_FLAG_OVERRIDE | SETANIM_FLAG_HOLD, 0);
				G_SoundOnEnt(ent, CHAN_ITEM, "sound/weapons/grapple/hookfire.wav");
				ent->client->hookDebounceTime = level.time + 0;
			}
		}
		else if (client->hook && (client->fireHeld == qfalse ||
			!(ucmd->buttons & BUTTON_GRAPPLE) ||
			(ucmd->buttons & BUTTON_USE) ||
			(ent->client->ps.pm_type == PM_DEAD)))
		{
			Weapon_HookFree(client->hook);
		}
	}
	else
	{
		if (client->hook && (client->fireHeld == qfalse ||
			!(ucmd->buttons & BUTTON_GRAPPLE) ||
			(ucmd->buttons & BUTTON_USE) ||
			(ucmd->buttons & BUTTON_BLOCK) ||
			(ent->client->ps.pm_type == PM_DEAD)))
		{
			Weapon_HookFree(client->hook);
		}

		if (!(ent->client->ps.eFlags & EF_FIRING))
		{
			client->fireHeld = qfalse;
		}
	}

g_client.cpp in void ClientDisconnect( int clientNum ).


void ClientDisconnect( int clientNum )
{
	gentity_t	*ent;

	ent = g_entities + clientNum;
	if ( !ent->client )
	{
		return;
	}

	gi.unlinkentity (ent);
	ent->s.modelindex = 0;
	ent->inuse = qfalse;
	ClearInUse(ent);
	ent->classname = "disconnected";
	ent->client->pers.connected = CON_DISCONNECTED;
	ent->client->ps.persistant[PERS_TEAM] = TEAM_FREE;
	if (ent->client->hook)
	{ // free it!
		Weapon_HookFree(ent->client->hook);
	}

	gi.SetConfigstring( CS_PLAYERS + clientNum, "");

	IIcarusInterface::GetIcarus()->DeleteIcarusID(ent->m_iIcarusID);

}

g_combat.cpp in void player_die.


	if (self->client->hook)
	{ // free it!
		Weapon_HookFree(self->client->hook);
	}

and finally g_missile.cpp.


#define MISSILE_PRESTEP_TIME 50
/*
=================
fire_grapple
=================
*/
gentity_t *fire_grapple(gentity_t *self, vec3_t start, vec3_t dir)
{
	gentity_t	*hook;

	VectorNormalize(dir);

	hook = G_Spawn();
	hook->classname = "hook";
	hook->nextthink = level.time + 10000;
	hook->think = Weapon_HookFree;
	hook->s.eType = ET_MISSILE;
	hook->svFlags = SVF_USE_CURRENT_ORIGIN;
	hook->s.weapon = WP_MELEE;
	hook->ownerNum = self->s.number;
	hook->methodOfDeath = MOD_ELECTROCUTE;
	hook->clipmask = MASK_SHOT;
	hook->parent = self;
	hook->target_ent = NULL;
	hook->s.pos.trType = TR_LINEAR;
	hook->s.pos.trTime = level.time - MISSILE_PRESTEP_TIME;
	hook->s.otherEntityNum = self->s.number;
	VectorCopy(start, hook->s.pos.trBase);
	VectorScale(dir, 900, hook->s.pos.trDelta);
	SnapVector(hook->s.pos.trDelta);
	VectorCopy(start, hook->currentOrigin);
	self->client->hook = hook;

	return hook;
}

These are the only places where Weapon_HookFree is being called?

​​​

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...