LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW & MVTec Halcon

Solved!
Go to solution

Then the library is not really multithreading safe. It probably assumes that it is always called from the same thread and uses possibly things like Thread Local Storage or similar for its internal operation. Nice idea but that works very badly with LabVIEW. You don't really have a way to tell in LabVIEW to call a specific code in a specific thread other than the UI thread.

 

If it is indeed TLS which causes this problem (there are several other possible causes) then you would have to restore the TLS environment every time on entering your functions before calling any Halcon functions, and save it back into a buffer that you maintain with your API before returning to LabVIEW. A somewhat work intensive thing to do, but it's at least possible.

 

But it's all speculation and not completely consistent, because the library obviously was meant to support some multi-threading operation in some ways.

Rolf Kalbermatter
My Blog
0 Kudos
Message 51 of 63
(6,306 Views)

@MTrivedi wrote:

Hello Rolf 

 

You are right but if I am using CLFN as a "run in any thread" then again old error of 9700 and hanging issue occured


That should not happen if everything is done correctly. I mean, you do some work with Halcon in LabVIEW and then your very last operator is FinalizeHALCONLibrary() and then you don't touch any Halcon-related things at all or exit LabVIEW completely. I tried this by myself and this works without any messages or something.

 

Why do you attach these large screenshots instead of copy-pasting the code as a text? Well, not knowing which operators you use I advise you to check every operator with its description from Halcon Help. There you could find the information whether your operator is really reentrant or not. For instance, here's the info about GetContourXld:

Parallelization

  • Multithreading type: reentrant (runs in parallel with non-exclusive operators).
  • Multithreading scope: global (may be called from any thread).
  • Processed without parallelization.

 

0 Kudos
Message 52 of 63
(6,303 Views)

Just tried this very simple code:

 

#include "HalconCpp.h"

using namespace HalconCpp;

extern "C" __declspec(dllexport) void __stdcall ReadImg()
{
  HImage neu;
  ReadImage(&neu,"C:/444.png");
}

extern "C" __declspec(dllexport) void __stdcall ShutDown()
{
  FinalizeHALCONLibrary();
}

Used MSVS 2008 and LabVIEW 2017 32-bit. My block diagram looks like this:

2017-11-06_15-01-36.jpg

 

 

 

It runs like a clockwork.

0 Kudos
Message 53 of 63
(6,296 Views)

For the clarity I need to add... You should understand what happens when you run the same VI after you executed it one time and didn't closed it. Your DLL and Halcon related libraries are still in memory. But FinalizeHALCONLibrary() has been called. The documentation states:


Please note that once FinalizeHALCONLibrary() has been called, no further HALCON functions may be called.

So now you have to unload your DLL from memory with closing your VI (or closing LabVIEW or calling FreeLibrary explicitly). If you don't do that and call Halcon functions again, the threads stay alive and FinalizeHALCONLibrary() doesn't work anymore! Now if you close the VI or LabVIEW, you get that annoying 9700 message. A fairly obvious solution in this situation would be calling FP.Close at the very end of your program in IDE mode or calling "Quit LabVIEW" in RTE mode. Of course, you could play with LoadLibrary / FreeLibrary if you feel that it's uncomfortably to see the program closing after every run. There might be another ways around that (say, manually calling shutdown CLFN when you finished your work).

0 Kudos
Message 54 of 63
(6,290 Views)

Hello all,

 

My first issue is solved when I am using single thread halcon DLL with run in any thread and terminate / unload it while closing labview.

 

But now the problem is there when I am using same halcon DLL function multiple times parallel in labview and terminate them when I closed labview , at that time again the issue of error 9700 occurred. 

I set system command "Set system("parallelize_operators","true")

In my halcon DLL for parallel execution 

And in labview call it by using CLFN in run in any thread and set vi properties "preallocated clone reentrant execution"

 

So how to solve it. I have to run my DLL parallel so any solution or other operation? 

 

In this case "FinalizeHalconLibrary" didn't work to unload all DLL I called multiple times or parallel

0 Kudos
Message 55 of 63
(6,253 Views)

MTrivedi wrote:

But now the problem is there when I am using same halcon DLL function multiple times parallel in labview


Do you mean this?

2017-11-08_12-44-31.jpg

Or are you calling SomeFunc1 several times (SomeFunc1 = SomeFunc2 = SomeFunc3 on the picture)?

0 Kudos
Message 56 of 63
(6,249 Views)

Yes exactly but still same error occurred

0 Kudos
Message 57 of 63
(6,239 Views)

Did you tried this approach?

1. Init section: hDLL = LoadLibrary(/* path to your DLL here */)

2. Work section: do what you do here

3. Final section: ShutDown / FinalizeHALCONLibrary, then FreeLibrary(hDLL)

If still not working, then try without step 3.

Well, if this doesn't work for you again, then just ignore this 9700 message and use FinalizeHALCONLibrary in RTE (EXE) only.

After all, you could try to switch to .NET nodes instead of DLLs. There's no such problem with the threads, as all the cleanup is done automatically. You have to close each reference when it's no longer needed. That's all.

0 Kudos
Message 58 of 63
(6,235 Views)

Well, I just had a quick test and this method doesn't eliminate 9700 message.


@dadreamer wrote:

Did you tried this approach?

1. Init section: hDLL = LoadLibrary(/* path to your DLL here */)

2. Work section: do what you do here

3. Final section: ShutDown / FinalizeHALCONLibrary, then FreeLibrary(hDLL)


But this one works:


@dadreamer wrote:

Did you tried this approach?

1. Init section: hDLL = LoadLibrary(/* path to your DLL here */)

2. Work section: do what you do here


2017-11-08_19-53-09.jpg

So, it seems that FinalizeHALCONLibrary is not working properly in LabVIEW as it should. I feel like I need to send a report to MVTec but this time I'm too lazy for it. Smiley Tongue

In fact, I don't even think that there's much meaning for using it in EXE because the process termination automatically shuts the threads down. No crashes or something to worry about.

 

I'm a little surprised that this message box still appears when 'do_low_error' parameter is set to 'false' or 'disabled'. It might be a bug or something specific, but I'm not going to puzzle it out.

0 Kudos
Message 59 of 63
(6,224 Views)

Hello , 

I need your help in halcon dll integration with LabVIEW. 

I have a question about how can I get  back processed image from halcon in LabVIEW ?

I passed LabVIEW image in halcon dll and some process on it , now I get back that image in LabVIEW . Please help me about it.

0 Kudos
Message 60 of 63
(6,098 Views)