01-26-2017 09:33 AM - edited 01-26-2017 09:41 AM
Hi all,
I am having a problem with managing .NET handles/instances in CVI program. When I repeatedly run certain function that sometimes declares new handles and sometimes uses preexisting handles, in first couple of runs no exceptions/errors, until ALL crashes and I have error messages that attached here.
During debug process I've noticed that after releasing a handle and then trying to instanciate a new one of the same type it returns with NULL value. Although a single run before, same sequence worked fine.
More over, I have small functions that explicitly release a static global handle and then initialize it with NULL. After returning from that function, the same handle has some value - other than NULL.
What can be a problem here?
How long a single function can be? (I know I have to break long functions into smaller ones - anyway, can it effect .NET handles that used in that function?)
How should I declare my .NET handles? As static globals? As stack locals? Or with struct pointer and a heap?
For now all my problematic handles declared as static globals. And I just manipulate release/instanciation of these handles.
If I remember correctly static global variable should maintain its value until the end of a program - but I can also change its value anytime during my program. Is there any "catch" with these statics?
I am trying not to overwrite handles/objects without releasing them first, or use a single object for all my operations.
Thank for your help in advance.
Solved! Go to Solution.
01-30-2017 05:57 PM
Hi Art,
Could you provide some of your code where you declare your handles and instantiate your .NET variables?
If you could narrow it down to one example that exhibits this behavior, that would be very helpful.
02-07-2017 01:16 AM
Hi Roxana,
Sorry for late answer, I was a bit off topic here. Anyway, code sample is attached here. It's a bit long, even after I have cut some of it.
Thanks for the help.
Best Regards,
Art
02-08-2017 04:35 PM
Hey Art,
Have you tried using this function instead:
http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/libref/cvicdotnetdiscardhandle/
02-09-2017 12:47 AM - edited 02-09-2017 12:48 AM
Hi Roxana,
Thanks for your support 🙂
I actually use this Discard function. Let me put it in more details. It goes like this
I defined the following macro :
#define FreeNotNULL(DotNetHandle) if((DotNetHandle)!=NULL)status=CDotNetDiscardHandle((DotNetHandle)); else
And I invoke the macro here (or sometimes in the middle of the code):
static int ReleaseDNetHndle(CDotNetHandle* Handle)
{
int status = 0;
SetBreakOnLibraryErrors (0);
FreeNotNULL(*Handle);
*Handle = NULL;
SetBreakOnLibraryErrors (1);
return status;
}
Why I did so, because I wanted to be sure that I actually released the handle, and than changed its value to NULL.
02-19-2017 09:46 AM
After consulting with Tektronix specialists, I was advised to use a Dispose method for each handle, that its class is also IClonable. After applying a Dispose method on each clonned object, and then releasing its handle the crashing was solved.