Need help troubleshooting my plugin

HouseBot Plugin Development Discussions.
Post Reply
dlmorgan999
HouseBot Special Member
Posts: 409
Joined: Tue Jul 13, 2004 9:13 am
Location: Tigard, OR

Need help troubleshooting my plugin

Post by dlmorgan999 »

I am trying to troubleshoot an issue in my plugin and I could use some help from the more experienced developers (I'm not really strong in C++). When I turn on debugging on my interface plugin I occasionally see lines like this in the log:


Oct 01 2005,10:09:38PM,Omni (network),Debug,"Security Mode Burglary in area 12 by code 57 (start)"


Here is the code that generates the message:


Code: Select all

/////////////////////////////////////////////////////////////////////////////////
// CHAISecurityMode
/////////////////////////////////////////////////////////////////////////////////
BOOL CHAISecurityMode::ProcessEvent()
{
	m_pInstance->TraceMessage( ttDebug, "Security Mode %s in area %d by code %d (%s)",
							 sec_text[(m_nEvent >> 12) & 0x0007],
							 (m_nEvent >> 8) & 0x000F,
							 m_nEvent & 0x00FF,
							 (m_nEvent & 0x8000) ? "start" : "end");

	CDataPack	Data;
	Data.AddData( "Mode", sec_text[(m_nEvent >> 12) & 0x0007] );
	
	m_pInstance->NotifySubscribedDevices( "HAI Security Mode", "", &Data );

	return( TRUE );		
}

Notice that the mode indexes into sec_text to get the text value. Here is the code that creates sec_text:

Code: Select all

const char *sec_text[] = {"Off", "Day", "Night", "Away", "Vacation", "Day Instant", "Night Delayed"};
const char *alarm_text[] = {"Burglary", "Fire", "Gas", "Auxiliary", "Freeze", "Water", "Duress", "Temp"};


The odd part is that the value "Burglary" does not exist in sec_text but rather in alarm_text. As I said I'm not real strong in C++ (especially the memory management aspects) but if I were to guess I would imagine that those two arrays are being created right next to each other in memory and I'm getting a value for mode that's higher than I should. I think I'll add some code to log the full value of m_nEvent (a bitmapped value containing all the information about the event) but I figured I would also ask for advice from others.



-- Dave
ScottBot
Site Admin
Posts: 2787
Joined: Thu Feb 13, 2003 6:46 pm
Location: Georgia (USA)
Contact:

Post by ScottBot »

Dave,



Your suspicion about the array actually getting data from the array next to it is probably correct. You have to be very careful when accessing arrays in C/C++, because there's nothing to prevent you from accessing memory from outside the bounds of the array. This can cause some really nasty bugs, especially when writing to array elements.



I'd suggest adding a check like:

Code: Select all

int ndx = (m_nEvent >> 12) & 0x0007;
if (ndx < 0 || ndx >= sizeof(sec_text))
   display error message here that displays m_nEvent to figure out how this happened
else
   Call TraceMessage.
Scott
dlmorgan999
HouseBot Special Member
Posts: 409
Joined: Tue Jul 13, 2004 9:13 am
Location: Tigard, OR

Post by dlmorgan999 »

Hi Scott,



I read this quite a while ago and got it fixed but I forgot to say thanks so THANKS! :)



-- Dave
Post Reply