Null pointer passed in to SWIG wrapper

Hi guys,

I am using CM9.2 in a REST API project where the POST requests need to impersonate with a service account and connect to CM9.2 (user identification is not sent in the POST request, hence the host server needs to connect to CM9.2 with a service account which is already set up in CM9.2.).

I have adapted the impersonation code from Microsoft:

https://support.microsoft.com/en-au/help/306158/how-to-implement-impersonation-in-an-asp-net-application

The impersonation works fine for the first POST request (through PostMan), however any subsequent POST requests give me this strange error 'Null pointer passed in to SWIG wrapper', and fails to connect to CM9.2.

Following is the log dump for the two consecutive requests. The first one completed everything successfully, but the second one failed. Any idea?

======================================================================

2019-02-05 09:54:50,346 - ******************************************************************************
2019-02-05 09:54:50,362 - Initializing CM9.2 application base.
2019-02-05 09:54:50,917 - Setting CM9.2 service type as a web service.
2019-02-05 09:54:51,227 - ******************************************************************************
2019-02-05 09:54:51,227 - Received POST request from ::1.
2019-02-05 09:54:51,247 - Reverting to self was successful!
2019-02-05 09:54:51,302 - LogonUserA was successful!
2019-02-05 09:54:51,302 - Token duplication was successful! Token: 2568, duplicate token: 2576.
2019-02-05 09:54:51,302 - Impersonation context: System.Security.Principal.WindowsImpersonationContext.
2019-02-05 09:54:51,317 - Impersonation context is not null, means impersonation was successful!
2019-02-05 09:54:51,317 - Closing token handle was successful!
2019-02-05 09:54:51,317 - Closing duplicate token handle was successful!
2019-02-05 09:54:51,317 - Impersonated user successfully!
2019-02-05 09:54:51,317 - Connecting to CM9.2.
2019-02-05 09:54:51,333 - Creating CM9.2 database instance.
2019-02-05 09:54:51,333 - cm9Db is null.
2019-02-05 09:54:51,364 - cm9Db instance created.
2019-02-05 09:54:51,380 - Setting parameters for workgroup server.
2019-02-05 09:54:52,579 - Reverted the Windows impersonation context to the original Windows user.
2019-02-05 09:54:52,579 - Connected to the specified workgroup server of CM9.2.
2019-02-05 09:54:53,468 - Finished POST request processing.
2019-02-05 09:54:53,472 - Disconnecting from CM9.2.
2019-02-05 09:54:53,483 - Disconnected successfully!
2019-02-05 09:54:58,633 - ******************************************************************************
2019-02-05 09:54:58,640 - Received POST request from ::1.
2019-02-05 09:54:58,645 - Reverting to self was successful!
2019-02-05 09:54:58,668 - LogonUserA was successful!
2019-02-05 09:54:58,673 - Token duplication was successful! Token: 2652, duplicate token: 2648.
2019-02-05 09:54:58,677 - Impersonation context: System.Security.Principal.WindowsImpersonationContext.
2019-02-05 09:54:58,695 - Impersonation context is not null, means impersonation was successful!
2019-02-05 09:54:58,710 - Closing token handle was successful!
2019-02-05 09:54:58,710 - Closing duplicate token handle was successful!
2019-02-05 09:54:58,710 - Impersonated user successfully!
2019-02-05 09:54:58,710 - Connecting to CM9.2.
2019-02-05 09:54:58,726 - Creating CM9.2 database instance.
2019-02-05 09:54:58,795 - An error occurred while processing the POST request. Error msg: Null pointer passed in to SWIG wrapper. Function: 'CSharp_HPfHPTRIMfSDK_TrimPropertySet_getErrorMessage___'. File: 'C:\9.2\trim\src\api\ApiTrimObjectGen_CSharp.cpp'. Line: 24989.
Error details:

Error timestamp: 05/02/2019 09:54:58
Error msg: Null pointer passed in to SWIG wrapper. Function: 'CSharp_HPfHPTRIMfSDK_TrimPropertySet_getErrorMessage___'. File: 'C:\9.2\trim\src\api\ApiTrimObjectGen_CSharp.cpp'. Line: 24989.

Error location:
Frame: 0
Declaring type: TrimPropertySet
Method: get_ErrorMessage

Frame: 1
Declaring type: Methods
Method: ConnectToCm9

Frame: 2
Declaring type: CM92MetadataController
Method: Post

======================================================================

 

Looking at the last successful log, the compiler didn't pass through the lines 2 onwards of the following code, means impersonation was successful at the second time, hence there must be something wrong with 'globals.cm9Db' which is a CM9.2 database object.

======================================================================

1: Globals.logger.Info($"Creating CM9.2 database instance.");

2: if (globals.cm9Db == null)
3: {
4: Globals.logger.Info("cm9Db is null.");
5: globals.cm9Db = new Database();
6: Globals.logger.Info("cm9Db instance created.");
7: }
8: if (globals.cm9Db.IsConnected)
9: {
10: Globals.logger.Info("cm9Db is connected.");
11: globals.cm9Db.Disconnect();
12: Globals.logger.Info("cm9Db is disconnected.");
13: }

======================================================================

  • Verified Answer

    Found the solution.

    I had to do two things.

    1) Make the database instance a static type:

    public static Database cm9Db;

     

    2) Set the database instance to NULL after POST processing.

    if (Globals.cm9Db == null)
    {
    Globals.logger.Info("CM9.2 was never connected. No need to disconnect.");
    return true;
    }
    if (Globals.cm9Db.IsConnected)
    {
    Globals.cm9Db.Disconnect();
    Globals.cm9Db.Dispose();
    Globals.cm9Db = null;
    Globals.logger.Info("Disconnected successfully!");
    }
    return true;