CallbackOnCollectedDelegate was detected

mlprosper
mlprosper used Ask the Experts™
on
One of my programmers is trying to write a demo component using a third-party library to process some forms we have in-house.  However, we keep running into an intermittent problem with a "CallbackOnCollectedDelegate was detected" error.  This occurs on the  myFormSet.Write(); method in the third party library.  From my research, this doesn't seem like a component issue, but rather a coding technique issue.  Could anyone shine some light on how to fix this issue?  We've already tried declaring the variables at the class level, but that doesn't work.  Would we also need to initialize the variable at the class level?  Here is the full error:
 
CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type 'PegasusImaging.WinForms.FormDirector1!PegasusImaging.WinForms.FormDirector1.WorkerImports+DesignRunProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

And the code for the method that causes the error:
 
AWSHelper objAWS = new AWSHelper();
FormDirector.FormDirector myFormDirector = new FormDirector.FormDirector();
FormDirector.FormSetFile myFormSet = new PegasusImaging.WinForms.FormDirector1.FormSetFile(myFormDirector);

try
{
	GatherBarCodeInformation(ref _ExamId, ref _StudentId, ref _PageId, ref _DistrictId, _filledImage);
	CreateForm(Convert.ToInt32(_ExamId), Convert.ToInt32(_PageId), ref myFormSet, myScanFix, objImagXView);

		if (myFormSet.FormDefinitions.Count > 0)
		{
			int myFormCount = myFormSet.FormDefinitions.Count;

			for (int i = 0; i < myFormCount; i++)
			{
				FormDirector.FormDefinitionFile myFormDef = (myFormSet.FormDefinitions[i] as FormDirector.FormDefinitionFile);
				if (myFormDef.Name == "")
				{
					myFormCount--;
					i--;
					myFormSet.FormDefinitions.Remove(myFormDef);
					myFormDef.Dispose();//1.0.8.0
				}
				else
				{
					myFormDef.Filename = TempFormSetPath + Path.GetFileName(myFormDef.Filename);
					myFormDef.Lock();
					myFormDef.Write();
					myFormDef.Unlock();

					objAWS.WritingASheet(objAWS.UnfilledBucketName, TempFormSetPath + Path.GetFileName(myFormDef.Filename));
					File.Delete(TempFormSetPath + Path.GetFileName(myFormDef.Filename));                                        
				   
					
				}
				myFormDef = null;
			}

			myFormSet.Filename = TempFormSetPath + Path.GetFileName(myFormSet.Filename);
			GC.KeepAlive(myFormSet);
			myFormSet.Lock();
			myFormSet.Write();
			myFormSet.Unlock();

			objAWS.WritingASheet(objAWS.UnfilledBucketName, TempFormSetPath + Path.GetFileName(myFormSet.Filename));
			objAWS = null;
			File.Delete(TempFormSetPath + Path.GetFileName(myFormSet.Filename));
		}
	}
 
	myFormSet.FormDefinitions.Add(new PegasusImaging.WinForms.FormDirector1.FormSetFile(myFormDirector).FormDefinitions[0]);
  
}
catch (Exception ex)
{
	ErrorTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:tt");
	ErrorMessage = "*Error:" + ex.Message;
}
finally
{
	myFormSet.Dispose();
	myFormSet = null;
	myFormDirector.Dispose();
	myFormDirector = null;
}

#endregion

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Independent Consultant
Commented:
My suggestion is that you use a using statement for anything implementing IDisposable.

Therefore,

using ( FormDirector.FormDirector myFormDirector = new FormDirector.FormDirector() )
 {
  using ( FormDirector.FormSetFile myFormSet = new PegasusImaging.WinForms.FormDirector1.FormSetFile(myFormDirector) )
 {
   // you code
  //  Totally get rid of the finally section in the try/catch.  Using calls .Dispose() for you.
 }
}

Author

Commented:
your comment pointed me in the right direction.  The solution was to place all the code for the FormDirector and formset variables in the same method and utilize the "using" statement.  Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial