Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Out-of-process C++ application causes intermittent crash of Inventor

1 REPLY 1
Reply
Message 1 of 2
Sanjay.Gangadhara
604 Views, 1 Reply

Out-of-process C++ application causes intermittent crash of Inventor

Hi there,

 

I've written a piece of code in C++ to open a part in Inventor and get parameter data from the part from out-of-process. This code works, but occasionally (i.e. randomly) execution of the code will result in an error message being returned from Inventor which indicates a crash of the program. I cannot figure out why, and because the problem occurs randomly I've had difficultly isolating the problem. Here is my code - any thoughts on what I might be doing wrong? The error suggests that I might be releasing memory that I don't own or something like that, but since it happens intermittently it is hard to know.

 

int error;
CComBSTR bsPartName;
CLSID clsid;
FILE *out;
HRESULT hres = NOERROR;
TCHAR szFileName[255];

/* Global Autodesk Inventor pointers */
CComPtr<Application> aiApp;
CComPtr<Document> aiDoc;

/* Get Inventor program ID */
CoInitialize(NULL);
hres = CLSIDFromProgID(OLESTR("Inventor.Application"), &clsid);

/* Open Inventor */
error = 0;
if (hres == S_OK)
	{
	hres = aiApp.CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER);
	if (hres == S_OK) aiApp->Visible = VARIANT_FALSE;
	else error = 1;
	}
else error = 1;

if (!error)
	{
	/* Get the part */
	_tcscpy(szFileName, _T("C:\\Temp\\Box.ipt"));
	_bstr_t sPartName(szFileName);
	bsPartName.AppendBSTR(sPartName);
	hres = aiApp->Documents->Open(bsPartName, VARIANT_TRUE, &aiDoc);
	if (hres != S_OK) error = 1;

	if (!error)
		{
		/* Open file for printing */
		out = fopen("C:\\Temp\\InventorTestLog.txt", "at");

		/* Get the part document pointer from the document pointer */
		CComQIPtr<PartDocument> aiPartDoc(aiDoc);
		if (aiPartDoc)
			{
			/* Define necessary variables */
			int ii;
			double dim_value;
			long ndim;
			_bstr_t bsParamName;
			TCHAR disp[255];
			TCHAR szParamName[255];
			_variant_t tempValue;

			/* Get the number of model parameters in the part */
			ndim = aiPartDoc->ComponentDefinition->Parameters->ModelParameters->Count;
	
			/* Get data for each parameter */
			for (ii = 1; ii <= ndim; ii++)
				{
				bsParamName = aiPartDoc->ComponentDefinition->Parameters->ModelParameters->Item[ii]->Name;
				_stprintf(szParamName, _T("%s"), (LPCTSTR)bsParamName);
				tempValue = aiPartDoc->ComponentDefinition->Parameters->ModelParameters->Item[ii]->Value;
				if (tempValue.vt == VT_R4 || tempValue.vt == VT_R8)
					{
					if (tempValue.vt == VT_R4) dim_value = tempValue.fltVal;
					else dim_value = tempValue.dblVal;
					}
				else dim_value = 0.0;
				if (out)
					{
					_stprintf(disp, _T("Parameter %s value = %.6f\n"), szParamName, dim_value);
					_fputts(disp, out);
					}
				}
			aiPartDoc.Release();
			}

		/* Close file for printing */
		if (out) fclose(out);
		}

	/* Close the part */
	if (aiDoc)
		{
		hres = aiDoc->Close(VARIANT_TRUE);
		aiDoc.Release();
		}
	}

/* Close Inventor */
if (aiApp)
	{
	aiApp->Quit();
	aiApp.Release();
	}
CoUninitialize();

 

Tags (3)
1 REPLY 1
Message 2 of 2

Hi Sanjay,

Is this issue resolved? If no, please try below:

 

//////////////////////////////////////////////////////////////////

int error;

CComBSTR bsPartName;

CLSID clsid;

FILE *out;

HRESULT hres = NOERROR;

TCHAR szFileName[255];

 

CoInitialize(NULL);

{

    /* Global Autodesk Inventor pointers */

    CComPtr<Application> aiApp;

    CComPtr<Document> aiDoc;

 

    /* Get Inventor program ID */

    hres = CLSIDFromProgID(OLESTR("Inventor.Application"), &clsid);

 

    //...

 

    /* Close Inventor */

    if (aiApp)

    {

        aiApp->Quit();

       aiApp.Release();

    }

}

CoUninitialize();

///////////////////////////////////////////////////////////////////

 

The problem I guess is that the COM pointers aiApp and aiDoc are destroyed (call ~CComPtr() function) after CoUninitialize().

In theory, it should be no problem because you have released them and the raw pointer should be NULL before code reaches CoUninitialize(). But things are not always perfect, the raw pointer seems is not NULL when call ~CComPtr() after CoUninitialize().

 

Try to move CoInitialize and CoUninitialize out the scope. ~CComPtr() will be called when code go out the scope to make sure it be done before CoUninitialize. Please refer to http://stackoverflow.com/questions/11249298/directshow-code-crashes-after-exit-pushsourcedesktop-sam...

Hope it can help you, Regards. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report