您的位置:首页 > 其它

一个有趣的bug

2010-08-18 17:42 225 查看
苦战了3个小时终于发现了问题所在,忍不住发泄一下,随便抽取我写的其中的两个类说明一下问题

在ScriptControl类中:

// ScriptControl.h: interface for the CScriptControl class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SCRIPTCONTROL_H__7B2C1ACD_6804_4DDA_9B0D_D4450AB44804__INCLUDED_)
#define AFX_SCRIPTCONTROL_H__7B2C1ACD_6804_4DDA_9B0D_D4450AB44804__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "pelcoAPI/soapScriptControlProxy.h"
class CScriptControl
{
public:
CScriptControl();
virtual ~CScriptControl();

int BeginScript(int mp_presetNum);
int EndScript(int mp_presetNum);
int ExecuteScript(int mp_presetNum);
int StopScript(int mp_presetNum);
int DeleteScript(int mp_presetNum);
char *m_IpAddress;
char *m_port;
private:
char *m_soap_endpoint;
char *m_scriptName;
//soap context
struct soap m_stuSoap;
//soap objects used to wrap soap message.
_ns7__BeginScript m_beginScript;
_ns7__BeginScriptResponse m_beginScriptResponse;
_ns7__EndScript m_endScript;
_ns7__EndScriptResponse m_endScriptResponse;
_ns7__ExecuteScript m_executeScript;
_ns7__ExecuteScriptResponse m_executeScriptResponse;
_ns7__HaltScript m_haltScript;
_ns7__HaltScriptResponse m_haltScriptResponse;
_ns7__DeleteScript m_deleteScript;
_ns7__DeleteScriptResponse m_deleteScriptResponse;
};
#endif // !defined(AFX_SCRIPTCONTROL_H__7B2C1ACD_6804_4DDA_9B0D_D4450AB44804__INCLUDED_)


在StreamControl类中:

// StreamControl.h: interface for the CStreamControl class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STREAMCONTROL_H__9BAFCDBD_B1CD_4E7A_A1D6_F33D04F30C33__INCLUDED_)
#define AFX_STREAMCONTROL_H__9BAFCDBD_B1CD_4E7A_A1D6_F33D04F30C33__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "pelcoAPI/soapStreamControlProxy.h"
class CStreamControl
{
public:
CStreamControl();
virtual ~CStreamControl();
int VideoPlay(const char *mp_pSessionId);
int VideoPause(const char *mp_pSessionId);
char *m_IpAddress;
char *m_port;
private:
char *m_soap_endpoint;
//soap context
struct soap m_stuSoap;
//soap play request objects wrapping soap message
_ns4__Play m_videoPlay;
_ns4__PlayResponse m_videoPlayResponse;
};
#endif // !defined(AFX_STREAMCONTROL_H__9BAFCDBD_B1CD_4E7A_A1D6_F33D04F30C33__INCLUDED_)


然后总体的manager类:

#ifdef CSVAPI
#else
#define CSVAPI _declspec(dllimport)
#endif // CSVAPI
class CSVAPI CPelcoManager
{
public:
CPelcoManager(char *mp_pchIPAddr, unsigned int mp_iPort);
virtual ~CPelcoManager();
public:
// all the webservice objects.
void * m_pCameraConfig;
void * m_pPositionControl;
void * m_pLensControl;
void * m_pStreamControl;
void * m_pVideoOutput;
void * m_pEncoderConfig;
void * m_pScriptControl;
void * m_pPelcoConfig;
public:
int GetCameraHue(int &iValue);
int SetCameraHue(int iValue);
int GetCameraBrightness(int &iValue);
int SetCameraBrightness(int iValue);
int GetCameraContrast(int &iValue);
int SetCameraContrast(int iValue);
int GetCameraSaturate(int &iValue);
int SetCameraSaturate(int iValue);
int GetTVFormat(int &iValue);
int SetTVFormat(int iValue);
char *GetVideoFormat();
char *GetDeviceType();
int GetVideoResolution(int &iValue);
int SetVideoResolution(int iValue);
int GetVideoFramerate(int &iValue);
int SetVideoFramerate(int iValue);
int GetVideoBitrate(int &iValue);
int SetVideoBitrate(int iValue);
int GetVideoQuality(int &iValue);
int SetVideoQuality(int iValue);
int GetSerialBaudrate(int &iValue);
int SetSerialBaudrate(int iValue);
int GetSerialStopNum(int &iValue);
int SetSerialStopNUm(int iValue);
int GetSerialCheckNum(int &iValue);
int SetSerialCheckNum(int iValue);
int GetSerialDataNum(int &iValue);
int SetSerialDataNum(int iValue);
int ControlPTZ(unsigned char *pPTZData);

int Play(unsigned long ulDestIp,unsigned short nPort);
int RefreshSession();
int Disconnect();
private:
void CreateObjects();
void InitializeObjects();
//distinguish stop command of focus , zoom , move (x,y)
int iStopCommandFlag;
char m_szPort[10];
char m_szIpAddr[20];
char m_szServerAddr[20];
};


在manager类中定了几个void *其实主要是为了生成dll而不包含头文件才这样做的,然后这样给我带来了致命的问题:

在讲void *指向对应的类对象时,定义如下:

m_pCameraConfig = new CCameraConfiguration();
m_pEncoderConfig = new CEncoderConfiguration();
m_pLensControl = new CLensControl();
m_pPositionControl = new CPositioningControl();
m_pStreamControl = new CScriptControl();
m_pVideoOutput = new CVideoOutput();
m_pScriptControl = new CScriptControl();
m_pPelcoConfig = new CPelcoConfiguration();


很明显大家可以看到,我将m_pStreamControl = new CScriptControl(); 错误的指向了ScriptControl类,

而我在调用时:

if (FALSE == ((CVideoOutput *)m_pVideoOutput)->VideoQuery())
{
return FALSE;
}
if (FALSE == ((CVideoOutput *)m_pVideoOutput)->VideoConnect(receivePort, 30))
{
return FALSE;
}
//send play request to receive the video stream
char temp[100];
memset(temp,0,100);
strcpy(temp,((CVideoOutput *)m_pVideoOutput)->m_strSessionId);
//
// 	HWND hwnd = ::GetForegroundWindow();
// 	if (temp != NULL)
// 	{
// 		::MessageBox(hwnd,temp,NULL,MB_OK);
// 	}
if (FALSE == ((CStreamControl *)m_pStreamControl)->VideoPlay(temp))
{
return FALSE;
}


调用时是通过强制转换来调用成员变量的,当然我在调用VideoPlay函数是失败的,

但是在debug时,却无法确认是此问题所导致,原因是通过添加断点可以得知,调用

可以进入函数内部,并且在函数内部走的很顺利,终于不得已,最后通过在构造函数内部添加断点,

通过一个未初始化的变量得知,是一开始就new出错了,哎,很悲剧,真是无语,解决后更是兴奋的无语 。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: