Home > Software engineering >  C development of opc client, the establishment of the asynchronous callback when there is a problem,
C development of opc client, the establishment of the asynchronous callback when there is a problem,

Time:09-18

//set up an asynchronous callback 
CComObject * pCOPCDataCallback;//callback object pointer
CComObject : : CreateInstance (& amp; PCOPCDataCallback);//by ATL templates to create a callback object instance

When execution to the error, error message: OPCTest. Exe of 0 x012c2f79 in untreated exception: 0 xc0000005: access conflict while reading position 0 x00000000,
Jump into atlcom interruption debugging.
in the h
 CComObject (_In_opt_ void *=NULL) throw () 
{
_pAtlModule - & gt; The Lock ();
}

Someone met this problem? O guide
So many points, really sorry

CodePudding user response:

 CComModule _Module; 
_pAtlModule=& amp; _Module;
CComObject * pCOPCDataCallback;//callback object pointer
CComObject : : CreateInstance (& amp; PCOPCDataCallback);//by ATL templates to create a callback object instance


Another: pCOPCDataCallback created the success of the reference count is zero, when used to increase the reference count AddRef, used up and then reduce the reference counting Release,

CodePudding user response:

Under the NULL pointer exception, you Debug to see if your pointer is NULL?

CodePudding user response:

reference 1st floor duan0783 response:
 CComModule _Module; 
_pAtlModule=& amp; _Module;
CComObject * pCOPCDataCallback;//callback object pointer
CComObject : : CreateInstance (& amp; PCOPCDataCallback);//by ATL templates to create a callback object instance


Another: pCOPCDataCallback created the success of the reference count is zero, when used to increase the reference count AddRef, to reduce the reference count, the Release,
I add the two sentences can pass, but finally Release resources and dance to the
 CComObject (_In_opt_ void *=NULL) throw () 
{
_pAtlModule - & gt; The Lock ();
}
//Set refcount - (LONG_MAX/2) to protect destruction and
//also catch mismatched Release in the debug builds
Virtual ~ CComObject throw () ()
{
M_dwRef=- (LONG_MAX/2);
FinalRelease ();
# ifdef _ATL_DEBUG_INTERFACES
_AtlDebugInterfacesModule. DeleteNonAddRefThunk (_GetRawUnknown ());
# endif
_pAtlModule - & gt; Unlock ();
}
Below the _pAtlModule - & gt; Unlock (); The
I release the source code:
HRESULT rl.
OPCHANDLE * phServer;
HRESULT * pErrors;
//delete Item
PhServer=new OPCHANDLE [1].
PhServer [0]=m_ItemResult [0]. HServer;
Rl=m_IOPCItemMgt - & gt; RemoveItems (1,//[in] to remove 1 item
PhServer,//[in] handle to the server
& PErrors);//[out] server returns the error code
If (rl!=S_OK & amp; & Rl!=S_FALSE)
{
//RemoveItems failure
} else if (rl==S_OK)
{
//RemoveItems success
}
The delete [] phServer;
CoTaskMemFree (pErrors);
CoTaskMemFree (m_ItemResult);
M_ItemResult=NULL;
//release the asynchronous interface
M_IOPCAsyncIO2 - & gt; Release ();
M_IOPCAsyncIO2=NULL;
//release the item management interface
M_IOPCItemMgt - & gt; Release ();
M_IOPCItemMgt=NULL;
//delete group object
Rl=m_IOPCServer - & gt; RemoveGroup (m_GrpSrvHandle, TRUE);
If (rl!=S_OK)
{
//RemoveGroup failure
} else {
//RemoveGroup success
}
M_GrpSrvHandle=NULL;
Rl=AtlUnadvise (m_IOPCGroupStateMgt IID_IOPCDataCallback, m_dwAdvice);
M_IOPCGroupStateMgt - & gt; Release ();
//release OPC server
M_IOPCServer - & gt; Release ();
M_IOPCServer=NULL;
//close the COM library
CoUninitialize ();

After deleting groups, can appear the error

CodePudding user response:

CodePudding user response:

The Debug under open the call stack, look at the function call stack, first find the error function call,

CodePudding user response:

CComObject : : CreateInstance (& amp; PCOPCDataCallback);//by ATL templates to create a callback object instance
The above statement is correct is not very clear, because according to the OPC it should be to create a callback object
So I usually write
COPCDataCallback * pCOPCDataCallback=new CComObject;
PCOPCDataCallback - & gt; AddRef ();
PCOPCDataCallback - & gt; FinalContruct ();

Also suggest that you don't call in general applications CoUninitialize () as the system will call, unless you know exactly how this thread needs to manage the COM object has been released in full

CodePudding user response:

Call back not to use, you need to use under the IConnectionPoint Advise with it, can't call like for other com,

CodePudding user response:

A great god, and when I perform AtlAdvise after establishing the callback didn't succeed, tip: can not find the specified object, export program, please know that this is how to return a responsibility? Thank you very much! For a long time have not solve,
The code is as follows:
CComModule _Module;
_pAtlModule=& amp; _Module;
CComObject * pCOPCDataCallback;
CComObject : : CreateInstance (& amp; PCOPCDataCallback);
LPUNKNOWN pCbUnk;
PCbUnk=pCOPCDataCallback - & gt; GetUnknown ();
DWORD dwCookie;
HRESULT hRes=AtlAdvise (pIOPCGroupStateMgt, pCbUnk IID_IOPCDataCallback, & amp; DwCookie);

CodePudding user response:

Use CComPtr Instead of CComObject *
  • Related