LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Managing .NET handles and instances

Solved!
Go to solution

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. 

Download All
0 Kudos
Message 1 of 6
(3,363 Views)

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. 

 

0 Kudos
Message 2 of 6
(3,303 Views)

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

0 Kudos
Message 3 of 6
(3,273 Views)

Hey Art,

 

Have you tried using this function instead: 

http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/libref/cvicdotnetdiscardhandle/

0 Kudos
Message 4 of 6
(3,247 Views)

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. 

 

0 Kudos
Message 5 of 6
(3,241 Views)
Solution
Accepted by topic author ArtB83

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.

0 Kudos
Message 6 of 6
(3,211 Views)