Discussion:
Call library function node only works with new VI
(too old to reply)
M. Shahini
2008-08-06 15:40:10 UTC
Permalink
This is my first time making a DLL and programming in C++, so please bear with me if I had made some fundamental mistakes.
I have made a DLL that reads images and performs Canny edge detection using the OpenCV libraries.
So I made a very simple LabVIEW program to test it (just a lib node with inputs and one output).
It worked fine, so I saved and quit. HOWEVER, when I restarted LabVIEW again and ran the same program,
it almost stopped working when the signal reached the Node and generated Error 1097:

----
Error 1097 occurred in Call Library Function Node in Canny.dll:
An exception occurred within the external code called by the Call Library Function Node.
The exception may have corrupted LabVIEW's memory.
Save any work to a new location and restart LabVIEW.
----

The only way I got it to work again was to:
1) copy and paste the program contents into a new file without any other modification, save and run.
2) change the DLL within the node configurations to any other DLL; this could also mean an exact copy of the DLL.
It worked once again, as long as LabVIEW.exe was not restarted; i.e. I could close the VI and reopen it
as many time as I wanted and it still works, but when I shut LabVIEW
off, restart it and reopen the same VI, it generates the above error.


I made a C++ program that calls the DLL, and it works fine, regardless of when and how many times it was called.
I also tried debugging it in Visual Studio and that worked fine too, regardless of when and how many times it was called.

Because of this, coupled with the fact that DLL works any amount of times in LabVIEW in a new VI, convinces me that
the settings of the node and the programming within the DLL code is
correct (code is still available on request, nothing propietary).

I have a feeling that it might have to do with how LabVIEW deals with its own memory preallocation/pointer validation
(apart from what is done in the DLL), or even a bug in LabVIEW, but I'm not sure.

Any help would be appreciated. Many thanks in advance.
smercurio_fc
2008-08-06 16:10:04 UTC
Permalink
It seems you're not the only one trying to <a href="http://forums.ni.com/ni/board/message?board.id=170&amp;thread.id=346737" target="_blank">play around</a> with the OpenCV library. Since you don't have anything proprietary, please post your suspect VI and we can try to take a look at it. P.S. Don't forget to post your DLL as well, though you will need to put it into a ZIP archive in order to post it. Please provide header files so we can see the function prototypes. Or, if you just want to post the code for the DLL that's fine also.Message Edited by smercurio_fc on 08-06-2008 10:42 AM
M. Shahini
2008-08-06 18:40:08 UTC
Permalink
Here is the VI, with the C++ file, header file and a settings file (settings.ini).

The ini file needs to be put in the same folder as the generated DLL.
It contains the folder that holds the images that need to be processed,
and the folder it outputs images too.

Eventually this will be replaced by a live feed from a camera,
but that shouldn't change the fact that I'm getting this weird error.

I also posted the problem on the LAVA forums&nbsp;&nbsp; <a href="http://forums.lavag.org/Calling-DLL-from-Call-Lib-Func-node-only-works-for-new-program-t11526.html" target="_blank">&gt;&gt; HERE &lt;&lt;</a>


CallLibFuncNode_files[7Aug08].zip:
http://forums.ni.com/attachments/ni/170/347227/1/CallLibFuncNode_files[7Aug08].zip
smercurio_fc
2008-08-06 20:10:05 UTC
Permalink
It seems that you're using Visual C++ 2005. I think that's your problem. When I compiled your code using Visual C++ 6.0 (after needing to make a few minor changes due to the library differences), I had no problem calling the DLL. I never got that error. To be honest, I don't have a lot of experience using Visual C++ 2005. The DLLs that I've created were done using 6.0. Anything else I've used .NET. You may want to check the Knowledge Base and this forum for any "gotchas" with Visual C++ 2005.
M. Shahini
2008-08-06 20:10:07 UTC
Permalink
Just making sure: are you sure you turned LabVIEW.exe off, restart it and run that same program again? My VI worked the first time I created it too -- after restarting LabVIEW,exe, it stopped working.Just to clarify:I am using LabVIEW 8.5,Visual C++ 2008 of Visual Studio ver.9.0.21022.8 RTM,Dev-CPP 4.9.9.2
M. Shahini
2008-08-06 20:10:05 UTC
Permalink
Actually, I first used Dev-CPP, but the error above happened. Then I switched to Visual Studio 2008 to try debugging it using "attach to process", which worked perfectly during debug.However, when I ran the same program using the DLL generated by VS, the error was still present.
smercurio_fc
2008-08-06 20:10:06 UTC
Permalink
Well, I have even less experience with Visual Studio 2008 since I don't have it. :smileyvery-happy: As I said, when I compiled the code you provided with Visual Studio 6.0 it worked just fine (after making those minor changes). I don't really know what else to tell you.
M. Shahini
2008-08-06 20:10:07 UTC
Permalink
Can you also specify the minor changes to made to the libraries? Sorry to be picky, but you have to understand that this has been baffling me for almost 2 weeks now.
smercurio_fc
2008-08-06 21:10:05 UTC
Permalink
Yes, I restarted LabVIEW each time. The mods I had to make were :- fopen_s -&gt; fopen
- sprintf_s -&gt; sprintf
- moved #include &lt;iostream&gt; to be called before #include "getpathdll.h" (I think this is VC++ 6.0 specific.)
- in fnGetPath had to declare i on its own line, as you cannot redeclare it in the second for-loop (again, a VC++ 6.0 issue).
Attached is the complete VC++ 6.0 project that I used, including the VI, in case it helps.Message Edited by smercurio_fc on 08-06-2008 03:46 PM


TestVSCanny.zip:
http://forums.ni.com/attachments/ni/170/347286/1/TestVSCanny.zip
M. Shahini
2008-08-06 21:10:06 UTC
Permalink
Thanks for all your help.On a whim, I tried making a LabVIEW 7.1 program and called the same DLL... it worked. (!?)This is very agonizing, but at least I got it to work consistently on a LabVIEW interface.Message Edited by M. Shahini on 08-06-2008 03:51 PM
smercurio_fc
2008-08-06 21:10:06 UTC
Permalink
Interesting. Especially since I was using 8.2. Maybe it's an 8.5 issue. I don't have 8.5 so I can't test it under 8.5.
Brian_A
2008-08-07 17:10:05 UTC
Permalink
Hi M. Shahini,I was unable to run your DLL (errors that had to do with dependencies, perhaps compiled in debug mode?) but was able to run the one compiled by smercurio_fc.&nbsp; Contrary to the results posted here, I received the SAME error in LabVIEW 8.5.1, 8.2, AND 7.1.&nbsp; The behavior seems to be inconsistent, so I would look to the DLL some operation that may be writing to memory that is not allocated.Also, it is best to wire all outputs (as well as inputs) of a Call Library Function Node.&nbsp; This is covered in <a href="http://digital.ni.com/public.nsf/allkb/58596F5D41CE8EFB862562AF0074E04C?OpenDocument" target="_blank">this Knowledge Base article</a>.
Loading...