XTarget related crash

A forum for reporting bugs NOT related to custom plugins.

Moderator: MacroQuest Developers

devestator
a ghoul
a ghoul
Posts: 121
Joined: Thu Feb 27, 2003 4:25 pm

XTarget related crash

Post by devestator » Mon May 17, 2010 10:58 am

Hey, sorry if this is not in the correct section. I forgot to get the actual plugin name that this function is in.

I tracked this bug down last night but didn't have time to fully go into finding a solution, and am not sure what the proper solution would be offhand. Not going to have time to really look into it again personally for a few days so figured I'd post the information here in case someone saw a quick fix for it.

The crash itself only happens when there are 5 XTargets, and only when messing with information on the 5th xtarget. So passing the ID of 5 to the xtarget like ${Me.XTarget[5].Type}

It's only happening to me in one particular loop, and others have said they have loops that work fine with 5 XTargets, so it could be something related to the sequence of information accessed about the XTarget, as I said I haven't had time to do more tracking or testing yet.

This Function:

Code: Select all

bool MQ2XTargetType::GETMEMBER()
{
    if(!VarPtr.Ptr)
        return false;
    if(PMQ2TYPEMEMBER pMember=MQ2XTargetType::FindMember(Member))
    {
        PXTARGETDATA xtd = (PXTARGETDATA)VarPtr.Ptr;

        switch((xTargetMembers)pMember->ID)
        {
        case Type:
            {
                char *pType = GetXtargetType(xtd->xTargetType);
                Dest.Ptr = pType[0] ? pType : "UNKNOWN";
                Dest.Type = pStringType;
                return true;
            }
        case ID:
            Dest.DWord = xtd->SpawnID;
            Dest.Type = pIntType;
            return true;
        case Name:
            Dest.Ptr = xtd->Name[0] ? xtd->Name : "NULL";
            Dest.Type = pStringType;
            return true;
        }
    }
    return false;
};
This line:

Code: Select all

                Dest.Ptr = pType[0] ? pType : "UNKNOWN";
Is where the error occurs. The error message visual studio gives me:

Code: Select all

First-chance exception at 0x02b242b6 (MQ2Main.dll) in eqgame.exe: 0xC0000005: Access violation reading location 0x00000000.
I can repeat this 100% of the time using the latest source code downloaded here. Here is the loop I'm running into the problem in, in case it helps:

Code: Select all

:nextAdd	
	/varset sID 0
	/varset nCount ${Math.Calc[${nCount}+1]}	
	/if (!${xTargetOnly} && ${lTargCount} >= ${haterCount}) {
		/varcalc nNCount ${nCount} - ${haterCount}
		/if (${nNCount} < 0) /varset nNCount 0
		/if (${checkID} == ${Me.ID}) {
			/varset sID ${Me.NearestSpawn[${nNCount},npc radius ${cDist} range ${minMobLvl} ${maxMobLvl} los noalert ${alertList}].ID}
		} else {
			/varset sID ${Spawn[${checkID}].NearestSpawn[${nNCount},npc radius ${cDist} range ${minMobLvl} ${maxMobLvl} noalert ${alertList}].ID}
		}
	} else {
		/varset nNCount ${nCount}
		
		/if (${Me.XTarget[${nCount}].Type.Equal[Auto Hater]} && ${nCount} < 6) {
			/if (${Me.XTarget[${nCount}].ID} && ${Spawn[${Me.XTarget[${nCount}].ID}].Distance} < ${cDist} && ${Spawn[${Me.XTarget[${nCount}].ID}].LineOfSight}) {
				/varset sID ${Me.XTarget[${nCount}].ID}
			} else /if (${Me.XTarget[${nCount}].ID}) {
				/goto :nextAdd
			}
		} else /if (${nCount} < 6) {
			/goto :nextAdd
		} else /if (${xTargetOnly}) {
			/call CorpseCheck
			/return
		}
	}
I know this is not the complete loop, but in the specific test conditions I was using the xtargets were always out of Line of Sight, so in that case this is the complete loop. A quick run down on the relevant variables that you don't see set here, xTargetOnly is a boolean true or false (obviously this is an add detection routine, if xTargetOnly is false it will check for additional adds beyond ${haterCount} via spawn search methods). haterCount is determined before this loop in a small loop that just adds up the total number of ${XTarget[n].Type.Equal[Auto Hater]} (in my case haterCount is always 5)

${lTargCount} is determined in the CorpseCheck routine and is the number of currently live targets being tracked as adds.

I think the other variables are self explanatory by their context. If you need more information from me, just ask! And if no one has time before Wednesday or Friday I will go back to hunting the solution myself and if I find one will post it here for others.

Dev

User avatar
ieatacid
Developer
Developer
Posts: 2727
Joined: Wed Sep 03, 2003 7:44 pm

Re: XTarget related crash

Post by ieatacid » Mon May 17, 2010 4:37 pm

Try changing this

Code: Select all

Dest.Ptr = pType[0] ? pType : "UNKNOWN";
to this

Code: Select all

Dest.Ptr = pType ? pType : "UNKNOWN";

devestator
a ghoul
a ghoul
Posts: 121
Joined: Thu Feb 27, 2003 4:25 pm

Re: XTarget related crash

Post by devestator » Tue May 18, 2010 12:15 am

That seems to have done the trick, thanks!

If you don't mind me asking why does it fix? I would like to know for future reference, also if not the same answer why did it work fine for up to 4 xtargets but not the 5th?

Thanks again!

Dev

User avatar
ieatacid
Developer
Developer
Posts: 2727
Joined: Wed Sep 03, 2003 7:44 pm

Re: XTarget related crash

Post by ieatacid » Tue May 18, 2010 4:20 pm

This dereferences the pointer

Code: Select all

pType[0]
if the pointer is zero (i.e. if GetXtargetType returns zero) it crashes.
why did it work fine for up to 4 xtargets but not the 5th?
I don't know. What target type was the 5th one in EQ?

devestator
a ghoul
a ghoul
Posts: 121
Joined: Thu Feb 27, 2003 4:25 pm

Re: XTarget related crash

Post by devestator » Tue May 18, 2010 7:18 pm

All 5 were always Auto Hater, that is one reason why it is so weird. I was thinking it may have had something to with one array having a lowerbound of 0 while another had a lowerbound of 1 so the 5th xtarget only, would be returning null on some data, but that was just an incorrect guess on my part.

I've never worked in c++ much to get fluent with all the pointers and stuff in it, always been more at home in VB myself, especially since it's what I use a lot at work. But I eventually want to get more into c++ programming, that is why I'm interested in the reasons behind the change working :)










xyilla
naggy
naggy
Posts: 33673
Joined: Sun Feb 23, 2025 5:36 am

Re: XTarget related crash

Post by xyilla » Wed Jan 14, 2026 3:36 am