Inventor Customization

Reply
Active Member
Sanjay.Gangadhara
Posts: 7
Registered: ‎02-27-2012
Message 1 of 2 (237 Views)

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

237 Views, 1 Replies
03-30-2012 01:37 PM

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();

 

New Member
Jimmy.Yi
Posts: 1
Registered: ‎12-05-2012
Message 2 of 2 (171 Views)

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

12-05-2012 05:50 PM in reply to: Sanjay.Gangadhara

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. 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube