AnsweredAssumed Answered

Need help w/ Unhandled Exception (PI-SDK in C++)

Question asked by StructInt1 on Jun 9, 2015
Latest reply on Jun 9, 2015 by StructInt1

I have been struggling with this problem for more than a week, any *hint* of an insight would be appreciated!

 

I am adding PI connectivity to an existing engineering application.  The App is in MS Visual C++ 2010, the main app is built using MFC, with most of the calculation done in a supporting DLL (C++, no MFC).  Of course, it is the DLL that needs to access the PI data :^>.  And unfortunately, due to the massive number of lines of code involved, re-writing the app in C# is a non-starter.

 

I have added the new functionality, and it successfully reads from/writes to the PI server, the calculations work, BUT at the end of the calculation, I try to release the PI-SDK 'pointer' (actually a COM interface ptr) and my application throws a fatal error.

     "Unhandled Exception 0xC0000005: Access Violation writing location 0x00000014."

To me, this error seems to indicate that something is going serioulsy wrong in tracking the references to COM pointers--but I am not very experieced in COM, so I may well be wrong about that.

 

I need to be able to resolve this error, or we will not be able to complete and deliver the application.  My preference would be to fix whatever issue is causing this, but an acceptable fallback would be to change the way we're connecting to PI (for instance, I suspect that we could do what we need with the PI-API interface, and this issue would go away)

 

I have previously found several example projects of PI-SDK in C++, and used them to build a 'module' within the calculation DLL to manage data transfer from/to the PI server.  It consists of a static class that acts as an interface layer between the rest of my code and the PI-SDK, and a few functions that use the interface class to retrieve/store data for the calculations:

 

[Calculation fns.]  ==calls=>  [new interface fns.]  ==calls=>  ["PI_link" class]  ==calls=>  [PI-SDK functions]

 

The class declaration is really simple:

 

class PI_link

{

public:

    PI_link();        // needs a constructor

    ~PI_link();         // and a destructor

    short  Init ();

    bool  Connect (LPCTSTR serverID, LPCTSTR LoginCmd);

    bool  IsConnected();

    void  Release ();    // == Un-Connect()

    void  Destroy ();    // == Un-Init()

 

    static const char * TRD_PItag (int UnitNo, LPCTSTR LocID, short TrdCol, bool alloc = false);

 

    short  read_config ();

    short  check_points (LPCTSTR ptIDs[]);

    short  get_1stLast_dates (int i_instr, COleDateTime *first, COleDateTime *last);

    short  read_1hr_data (int i_instr, DATE curdate);

    short  write_datapt (LPCTSTR pointID, COleDateTime tmstamp, float value);

    short  add_Point (LPCTSTR pointID);

 

protected:

    /* PI-SDK 'global' objects */ 

    IPISDKPtr    spPISDK;        // the PISDK //

    ServerPtr    spServer;        // the server //

 

    PIPointPtr GetPoint(LPCTSTR pointID) {

        if (!spServer) return NULL;

        return spServer->PIPoints->GetItem(pointID);

    }

};

 

I have traced through the code in the debugger, and it fails while executing the following function (in _my_ code):

 

void PI_link::Destroy()

{

    if ( IsConnected() )

        Release();        // contingency

 

    if (spPISDK != NULL) {

        //BUGBUG: this SEEMS right, but it was causing memory errors!!!

        //spPISDK->Release();

        spPISDK = NULL;     <== fails while executing this line

    }

}

 

The stack trace when the UE occurs shows that the access violation is occurring while the PI "session" is terminating:

 

call_stack.png

 

Again, I would appreciate any thoughts or insights, basically I'll try anything to get this working cleanly!

 

Curt Carney

Structural Integrity Assoc.

Outcomes