hi,

Im working on an .ocx control to integrate a 3rd party video vendor API into our product. Im stuck on an access violation error:
Unhandled exception in iexplorer.exe(PVCODEC.DLL): OxC0000005: Access Violoation. The ocx has been working for the past few days as I've been writting the wrapper. As of this morning Im getting that error. I've been pulling my hair out for the past 3 hours trying to find the cause of this, to no avail. I am attaching my ctrl.cpp code. If anyone has any idea what I am doing wrong can you please advise?

Thanks!

Code:
// PelcoDX8100Ctl.cpp : Implementation of the CPelcoDX8100Ctrl ActiveX Control class.

#include "stdafx.h"
#include "PelcoDX8100.h"
#include "PelcoDX8100Ctl.h"
#include "PelcoDX8100Ppg.h"
#include "resource.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const int NONE_DISPLAY_MODE		= 0;
const int LIVE_DISPLAY_MODE     = 1;
const int PLAYBACK_DISPLAY_MODE = 2;

bool g_bPaused = false;

COverlayWnd* g_pOverlayWnd = NULL;  //global overlay window
CPelcoDX8100Ctrl* g_pMyCtrl = NULL;

LPST_SERVER_BASE_INFO g_pServerInfo;

SYSTEMTIME CPelcoDX8100Ctrl::m_stLastReceivedTime;

#define TID_SERVER_CONNECT	0x1


IMPLEMENT_DYNCREATE(CPelcoDX8100Ctrl, COleControl)

/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CPelcoDX8100Ctrl, COleControl)
	//{{AFX_MSG_MAP(CPelcoDX8100Ctrl)
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CPelcoDX8100Ctrl, COleControl)
	//{{AFX_DISPATCH_MAP(CPelcoDX8100Ctrl)
	DISP_PROPERTY_EX(CPelcoDX8100Ctrl, "PTZSpeed", GetPTZSpeed, SetPTZSpeed, VT_I4)
	DISP_PROPERTY_EX(CPelcoDX8100Ctrl, "PlaybackSpeed", GetPlaybackSpeed, SetPlaybackSpeed, VT_I4)
	DISP_PROPERTY_EX(CPelcoDX8100Ctrl, "StartTime", GetStartTime, SetStartTime, VT_R8)
	DISP_PROPERTY_EX(CPelcoDX8100Ctrl, "PresetId", GetPresetId, SetPresetId, VT_I2)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Connect", Connect, VT_I4, VTS_BSTR VTS_I2 VTS_BSTR VTS_BSTR)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Focusin", Focusin, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Focusout", Focusout, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Irisclose", Irisclose, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Irisopen", Irisopen, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PrptStop", PrptStop, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Ptzdown", Ptzdown, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Ptzleft", Ptzleft, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Ptzright", Ptzright, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Ptzstop", Ptzstop, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Ptzup", Ptzup, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Zoomin", Zoomin, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Zoomout", Zoomout, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Presetgoto", Presetgoto, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Presetset", Presetset, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Disconnect", Disconnect, VT_I4, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PtzDL", PtzDL, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PtzDR", PtzDR, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PtzUL", PtzUL, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PtzUR", PtzUR, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "GetCameraId", GetCameraId, VT_I4, VTS_BSTR)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "BeginLiveStream", BeginLiveStream, VT_EMPTY, VTS_BSTR)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PlaybackPlay", PlaybackPlay, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Pause", Pause, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PlaybackReverse", PlaybackReverse, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PlaybackForward", PlaybackForward, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PlaybackStop", PlaybackStop, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PlaybackFirst", PlaybackFirst, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "PlaybackLast", PlaybackLast, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "Presetreset", Presetreset, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "ChangePlaybackSpeed", ChangePlaybackSpeed, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CPelcoDX8100Ctrl, "ViewLive", ViewLive, VT_EMPTY, VTS_NONE)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CPelcoDX8100Ctrl, COleControl)
	//{{AFX_EVENT_MAP(CPelcoDX8100Ctrl)
	EVENT_CUSTOM("Connected", FireConnected, VTS_I4)
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

BEGIN_PROPPAGEIDS(CPelcoDX8100Ctrl, 1)
	PROPPAGEID(CPelcoDX8100PropPage::guid)
END_PROPPAGEIDS(CPelcoDX8100Ctrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CPelcoDX8100Ctrl, "PELCODX8100.PelcoDX8100Ctrl.1",
	0xd302d9aa, 0x408f, 0x4429, 0x9e, 0x92, 0x5d, 0xe7, 0xde, 0xb4, 0x8, 0xa6)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CPelcoDX8100Ctrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DPelcoDX8100 =
		{ 0xc7469f9a, 0x2c8b, 0x4c6b, { 0xbe, 0x4c, 0xe4, 0x96, 0xde, 0x44, 0xd1, 0x84 } };
const IID BASED_CODE IID_DPelcoDX8100Events =
		{ 0x21eb31d7, 0x7100, 0x4763, { 0xb2, 0xdf, 0x41, 0x47, 0x3a, 0xf2, 0x31, 0x4e } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwPelcoDX8100OleMisc =
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CPelcoDX8100Ctrl, IDS_PELCODX8100, _dwPelcoDX8100OleMisc)


/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::CPelcoDX8100CtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CPelcoDX8100Ctrl

BOOL CPelcoDX8100Ctrl::CPelcoDX8100CtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_PELCODX8100,
			IDB_PELCODX8100,
			afxRegApartmentThreading,
			_dwPelcoDX8100OleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// Licensing strings

static const TCHAR BASED_CODE _szLicFileName[] = _T("PelcoDX8100.lic");

static const WCHAR BASED_CODE _szLicString[] =
	L"Copyright (c) 2010 Bold Technologies";


/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::CPelcoDX8100CtrlFactory::VerifyUserLicense -
// Checks for existence of a user license

BOOL CPelcoDX8100Ctrl::CPelcoDX8100CtrlFactory::VerifyUserLicense()
{
	return AfxVerifyLicFile(AfxGetInstanceHandle(), _szLicFileName,
		_szLicString);
}

/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::CPelcoDX8100CtrlFactory::GetLicenseKey -
// Returns a runtime licensing key

BOOL CPelcoDX8100Ctrl::CPelcoDX8100CtrlFactory::GetLicenseKey(DWORD dwReserved,
	BSTR FAR* pbstrKey)
{
	if (pbstrKey == NULL)
		return FALSE;

	*pbstrKey = SysAllocString(_szLicString);
	return (*pbstrKey != NULL);
}

const DWORD dwSupportedBits = 
      INTERFACESAFE_FOR_UNTRUSTED_CALLER |
      INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;

HRESULT STDMETHODCALLTYPE CPelcoDX8100Ctrl::XObjSafe::GetInterfaceSafetyOptions( REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions)
{
   METHOD_PROLOGUE(CPelcoDX8100Ctrl, ObjSafe)

   HRESULT retval = ResultFromScode(S_OK);

   IUnknown FAR* punkInterface;
   retval = pThis->ExternalQueryInterface(&riid, (void * *)&punkInterface);
   if (retval != E_NOINTERFACE) 
   {
      punkInterface->Release();
   }
   
   // We support both kinds of safety and always have both set regardless of the interface
   *pdwSupportedOptions = *pdwEnabledOptions = dwSupportedBits;
   return retval;
}

HRESULT STDMETHODCALLTYPE CPelcoDX8100Ctrl::XObjSafe::SetInterfaceSafetyOptions( REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions)
{
   METHOD_PROLOGUE(CPelcoDX8100Ctrl, ObjSafe)
   
   IUnknown FAR* punkInterface;
   pThis->ExternalQueryInterface(&riid, (void * *)&punkInterface);
   
   if (punkInterface) 
   {
      punkInterface->Release();
   }
   else 
   {
      return ResultFromScode(E_NOINTERFACE);
   }
   // Can't set bits we don't support.
   if (dwOptionSetMask & dwNotSupportedBits) 
   { 
      return ResultFromScode(E_FAIL);
   }

   dwEnabledOptions &= dwSupportedBits;
   if ((dwOptionSetMask & dwEnabledOptions) != dwOptionSetMask) 
   {
      return ResultFromScode(E_FAIL);
   }                                    
   
   // Don't need to change anything since we're always safe.
   return ResultFromScode(S_OK);
}

ULONG FAR EXPORT CPelcoDX8100Ctrl::XObjSafe::AddRef()
{
    METHOD_PROLOGUE(CPelcoDX8100Ctrl, ObjSafe)
    return pThis->ExternalAddRef();
}

ULONG FAR EXPORT CPelcoDX8100Ctrl::XObjSafe::Release()
{
    METHOD_PROLOGUE(CPelcoDX8100Ctrl, ObjSafe)
    return pThis->ExternalRelease();
}

HRESULT FAR EXPORT CPelcoDX8100Ctrl::XObjSafe::QueryInterface(REFIID iid, void FAR* FAR* ppvObj)
{
    METHOD_PROLOGUE(CPelcoDX8100Ctrl, ObjSafe)
    return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}

/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::CPelcoDX8100Ctrl - Constructor

CPelcoDX8100Ctrl::CPelcoDX8100Ctrl()
{
	InitializeIIDs(&IID_DPelcoDX8100, &IID_DPelcoDX8100Events);
	SetInitialSize(533,400);
	Initialize();
}

void CPelcoDX8100Ctrl::Initialize()
{
	g_pMyCtrl = this;
	m_hServer = NULL;
	m_iLastError = 0;
	m_ConnectTimeout = 15;
	m_iPTZSpeed = 0;
	m_dStartTime = 0.0;
	m_nDisplayMode = NONE_DISPLAY_MODE;
	m_nCameraId = -1;
	m_playbackSpeed = -1;
	m_sPresetId = -1;
	m_bPTZEnabled = false;
	m_nDivision = -1;
	g_bPaused = false;
	
}

/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::~CPelcoDX8100Ctrl - Destructor

CPelcoDX8100Ctrl::~CPelcoDX8100Ctrl()
{

	if (m_hServer)
	{	
		g_bPaused = true; 
		if(g_pOverlayWnd)
		{
			g_pOverlayWnd = NULL;
			delete g_pOverlayWnd;
			

			if(m_nDisplayMode == LIVE_DISPLAY_MODE)
			{
				DxStopLiveStream( m_hServer );
			}

			if(m_nDisplayMode == PLAYBACK_DISPLAY_MODE)
			{
				DxSearchStop( m_hServer );
			}
		}

		DxClose(m_hServer);
		m_hServer = NULL;
	}
}


/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::OnDraw - Drawing function

void CPelcoDX8100Ctrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{

	if(m_hWnd){

		if (!pdc)
			return;

		if(m_nDisplayMode != LIVE_DISPLAY_MODE && m_nDisplayMode != PLAYBACK_DISPLAY_MODE)
		{
			CString strTemp;
			strTemp = "NOT CONNECTED";

			pdc->FillRect(rcBounds, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE)));

			CRect oFrameRect;
			GetClientRect(&oFrameRect);

			pdc->DrawEdge(&oFrameRect, EDGE_BUMP, BF_RECT);
			pdc->SetTextColor(RGB(255,255,255));
			pdc->SetBkColor(RGB(054,154,37));
			pdc->DrawText(strTemp, strlen(strTemp),&oFrameRect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
		}
	}

}


/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::DoPropExchange - Persistence support

void CPelcoDX8100Ctrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

}

/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl::OnResetState - Reset control to default state

void CPelcoDX8100Ctrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange
}

/////////////////////////////////////////////////////////////////////////////
// CPelcoDX8100Ctrl message handlers


BOOL CPelcoDX8100Ctrl::OnSetExtent(LPSIZEL lpSizeL)
{
	// TODO: Add your specialized code here and/or call the base class
	if (g_pOverlayWnd && m_hWnd)
	{
		RECT rect;
		GetClientRect(&rect);
		
		g_pOverlayWnd->SetWindowPos(this, rect.top, rect.left, rect.right - rect.left, rect.top - rect.bottom, NULL);
		
	}

	return COleControl::OnSetExtent(lpSizeL);
	
}

void CPelcoDX8100Ctrl::InternalResize()
{
	int iWidth, iHeight;

	this->GetControlSize(&iWidth, &iHeight);
	InternalResize(iWidth,iHeight);
}

void CPelcoDX8100Ctrl::InternalResize(LONG width, LONG height)
{
	if(m_hWnd && g_pOverlayWnd)
	{
		CWnd * pWnd = CWnd::FromHandle(m_hWnd);
		pWnd->SetWindowPos(&wndTop,0,0,width,height,SWP_SHOWWINDOW);
		g_pOverlayWnd->SetWindowPos(&wndTop,0,0,width,height,SWP_SHOWWINDOW);
	}
}

long CPelcoDX8100Ctrl::Disconnect() 
{
	m_nDisplayMode = NONE_DISPLAY_MODE;
	g_bPaused = true; 

	if (m_hServer)
	{		
		if(g_pOverlayWnd)
		{
			delete g_pOverlayWnd;
			g_pOverlayWnd = NULL;

			if(m_nDisplayMode == LIVE_DISPLAY_MODE)
			{
				DxStopLiveStream( m_hServer );
			}

			if(m_nDisplayMode == PLAYBACK_DISPLAY_MODE)
			{
				DxPlaybackCtrl(m_hServer, _DXSDK_PLAYBACK_STOP);
				DxSearchStop( m_hServer );
			}
		}

		DxClose(m_hServer);
		m_hServer = NULL;
	}

	return 0;
}