EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  <20202021  2022  2023  2024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  <20202021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: RE: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug"
From: Freddie Akeroyd - UKRI STFC via Tech-talk <tech-talk at aps.anl.gov>
To: "tech-talk at aps.anl.gov" <tech-talk at aps.anl.gov>
Date: Tue, 19 May 2020 17:18:10 +0000

Hi Carsten,

 

I was just wondering if you had tried Michael’s suggestion of linking to ca.dll as you usually do, but adding a call to  

GetModuleHandleEx() with GET_MODULE_HANDLE_EX_FLAG_PIN to force it to ignore unloads of ca.dll? If that didn’t work, then it may be due to the way normal linking works. There is, however, an option to do delayed dll loading on the linker line

 

https://docs.microsoft.com/en-us/cpp/build/reference/linker-support-for-delay-loaded-dlls?view=vs-2019

 

This embeds a LoadLibrary / GetProcAddress sequence into the executable/dll to load the specified DLL only when the first function in it is called. As it is effectively mirroring the mechanism you had to implement by hand, I would hope combining this with use of  GET_MODULE_HANDLE_EX_FLAG_PIN  to ignore any FreeLibrary() calls that might occur from the main program would be equally successful. If that still doesn’t work, there look to be ways to modify the DLL helper function involved too.

 

Regards,

 

Freddie  

 

From: Tech-talk <tech-talk-bounces at aps.anl.gov> On Behalf Of Carsten Winkler via Tech-talk
Sent: 19 May 2020 14:39
To: Heesterman, Peter J <Peter.Heesterman at ukaea.uk>; Johnson, Andrew N. <anj at anl.gov>; tech-talk at aps.anl.gov; engbretson at anl.gov; Michael Davidsaver <mdavidsaver at gmail.com>
Subject: Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug"

 

Hi there,

the dirty workaround looks like that:

  • host app loads the client-dll (the wrapper library as CA Lab)
  • client-dll loads ca.dll and Com.dll via LoadLibrary() at DLL_PROCESS_ATTACH (do not link with them!)
  • client-dll must contain all definitions for structures and functions for loaded libraries :(
  • client-dll handles all channel access functions normally
  • on unload (DLL_PROCESS_DETACH), client-dll clean up all callback functions, subscriptions and channels (and more)
  • on unload (DLL_PROCESS_DETACH), neither ca.dll nor Com.dll will be unloaded (nor ca_context_destroy will be called)
  • ca.dll and Com.dll remain in the background
  • host app can load and unload client-dll as often as desired -> channel access functions will work
  • when host app exits, the OS must do all the cleanup work (that's the dirty part ;)

In my test case this workaround works well. But it is a huge effort to redefine all the structures and functions from the EPICS Base.

It would be great, if we could use caLib and ComLib in a better way.

Next few days I'll be on vacation.
Best regards
Carsten

Am 19.05.20 um 11:40 schrieb Heesterman, Peter J:

Hi Andrew, Carsten,

 

I may be able to help with this, if that would be beneficial.

 

I myself use caLab quite extensively.

 

Cheers,

 

Peter.

 

From: Tech-talk <tech-talk-bounces at aps.anl.gov> On Behalf Of Johnson, Andrew N. via Tech-talk
Sent: 18 May 2020 18:57
To: Carsten Winkler <carsten.winkler at helmholtz-berlin.de>
Cc: EPICS tech-talk <tech-talk at aps.anl.gov>
Subject: Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug"

 

Hi Carsten,

 

I’m not aware of anything that has actually been added to libCom or libca recently to prevent a completely shutdown; are you saying that you used to be able to unload the CA library and you can’t now? I don’t believe this has ever been possible (which is probably what Michael is also going by), but I might be wrong – please let us know what version of Base you were using if and when you could do that.

 

If it’s your requirement that has changed we would be happy to advise someone how to work out what needs to be modified to allow the complete unloading that you’re looking for, but I can’t promise that we’ll have core developer effort available to do the actual investigation and any development needed (we all manage our own time and have our own priorities controlled by our employers, so I can’t speak for the others).

 

- Andrew

 

 

On May 18, 2020, at 12:21 PM, Carsten Winkler via Tech-talk <tech-talk at aps.anl.gov> wrote:


thank you for your answer. This are bad news for our community. Many users are waiting for an EPICS Base upgrade for the Open Source software CaLab (interface beween LabVIEW and EPICS).

Best reganrds
Carsten

Am 18.05.2020 um 18:27 schrieb Michael Davidsaver:

libca and especially libCom can't be completely shutdown, and therefore
can't be safely unloaded.
 
 
On 5/18/20 8:34 AM, Carsten Winkler via Tech-talk wrote:
Hi,
 
for a project I need to load and unload the Channel Access context in a
shared library. Loading is no problem but unloading. It always fails
during ca_context_destroy() function.
 
Here is my demo code:
main.cpp (most simple case for a bigger project where it's necessary to
load and unload different shared libraries at runtime without
terminating main application)
----------------------------------------------------------
 
#include <windows.h>
#include <iostream>
 
int main()
{
    HINSTANCE hinstLib;
    BOOL fFreeResult;
    hinstLib = LoadLibrary(TEXT("myCaLib.dll"));
    if (hinstLib == NULL) {
        std::cout << "myCaLib.dll not found" << std::endl;
        return 1;
    }
    std::cout << "library loaded; hit ENTER to unload" << std::endl;
    getchar();
    fFreeResult = FreeLibrary(hinstLib);
    std::cout << "bye" << std::endl;
    return 0;
}
_______________________________________________________________________
 
myCaLib.cpp (most simple shared library with no functions except
creating and releasing the Channel Access context)
--------------------------------------------------------------
 
#include <iostream>
#include <cadef.h>
#include <windows.h>
 
BOOLEAN WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID
Reserved) {
    switch (nReason)
    {
        case DLL_PROCESS_ATTACH: {
            int32_t iResult =
ca_context_create(ca_enable_preemptive_callback);
            if (iResult != ECA_NORMAL) {
                std::cout << "Error: Could not create any instance of
Channel Access." << std::endl;
                return 0;
            }
            std::cout << "New instance of Channel Access context." <<
std::endl;
            std::cout << ca_context_status(ca_current_context(), 1) <<
std::endl;
            break;
        }
        case DLL_PROCESS_DETACH: {
            std::cout << "detach process ... " << std::endl;
            ca_context_destroy();
            break;
        }
    }
    return 1;
}
_______________________________________________________________________
 
build at command line at Windows 7 64 bit
--------------------------------------------------------------
 
%comspec% /k "C:\Program Files (x86)\Microsoft Visual
Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
 
set base=C:\base-7.0.x            <== directory of nightly build or
stable EPICS BASE 7.0 (built with "EPICS_HOST_ARCH=windows-x64")
cd C:\test_case\                       <== directory of our test sources
main.cpp and myCaLib.cpp
 
cl -nologo -FC -D__STDC__=0 -D_CRT_SECURE_NO_DEPRECATE
-D_CRT_NONSTDC_NO_DEPRECATE -Ox -GL -Oy- -W3 -MD -EHsc -DEPICS_BUILD_DLL
-DEPICS_CALL_DLL -I%base%/include/compiler/msvc
-I%base%/include/os/WIN32 -I%base%/include -c main.cpp myCaLib.cpp
 
link -nologo -subsystem:windows -dll -LTCG -incremental:no -opt:ref
-release -version:7.0 -MACHINE:X64 -libpath:%base%\lib\windows-x64
-out:myCaLib.dll -implib:myCaLib.lib myCaLib.obj ca.lib Com.lib
link -nologo -LTCG -incremental:no -opt:ref -release -version:7.0
-MACHINE:X64 -out:test.exe main.obj
 
set path=%base%\bin\windows-x64;%path%
test.exe
_______________________________________________________________________
 
test.exe will freeze because it hangs on ca_context_destroy()
 
I need any solution for this case, please.
 
Best regards
Carsten
 
 
________________________________
 
Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
 
Mitglied der Hermann von Helmholtz-Gemeinschaft Deutscher Forschungszentren e.V.
 
Aufsichtsrat: Vorsitzender Dr. Volkmar Dietz, stv. Vorsitzende Dr. Jutta Koch-Unterseher
Geschäftsführung: Prof. Dr. Bernd Rech (Sprecher), Prof. Dr. Jan Lüning, Thomas Frederking
 
Sitz Berlin, AG Charlottenburg, 89 HRB 5583
 
Postadresse:
Hahn-Meitner-Platz 1
D-14109 Berlin

 



Helmholtz-Zentrum Berlin für Materialien und Energie GmbH

Mitglied der Hermann von Helmholtz-Gemeinschaft Deutscher Forschungszentren e.V.

Aufsichtsrat: Vorsitzender Dr. Volkmar Dietz, stv. Vorsitzende Dr. Jutta Koch-Unterseher
Geschäftsführung: Prof. Dr. Bernd Rech (Sprecher), Prof. Dr. Jan Lüning, Thomas Frederking

Sitz Berlin, AG Charlottenburg, 89 HRB 5583

Postadresse:
Hahn-Meitner-Platz 1
D-14109 Berlin

 

-- 

Complexity comes for free, simplicity you have to work for.

 

 



Helmholtz-Zentrum Berlin für Materialien und Energie GmbH

Mitglied der Hermann von Helmholtz-Gemeinschaft Deutscher Forschungszentren e.V.

Aufsichtsrat: Vorsitzender Dr. Volkmar Dietz, stv. Vorsitzende Dr. Jutta Koch-Unterseher
Geschäftsführung: Prof. Dr. Bernd Rech (Sprecher), Prof. Dr. Jan Lüning, Thomas Frederking

Sitz Berlin, AG Charlottenburg, 89 HRB 5583

Postadresse:
Hahn-Meitner-Platz 1
D-14109 Berlin

This email and any attachments are intended solely for the use of the named recipients. If you are not the intended recipient you must not use, disclose, copy or distribute this email or any of its attachments and should notify the sender immediately and delete this email from your system. UK Research and Innovation (UKRI) has taken every reasonable precaution to minimise risk of this email or any attachments containing viruses or malware but the recipient should carry out its own virus and malware checks before opening the attachments. UKRI does not accept any liability for any losses or damages which the recipient may sustain due to presence of any viruses. Opinions, conclusions or other information in this message and attachments that are not related directly to UKRI business are solely those of the author and do not represent the views of UKRI.


References:
BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Carsten Winkler via Tech-talk
Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Michael Davidsaver via Tech-talk
Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Carsten Winkler via Tech-talk
Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Johnson, Andrew N. via Tech-talk
Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Carsten Winkler via Tech-talk

Navigate by Date:
Prev: PVI Prototype - a framework for specifying the interface to an EPICS driver in a single YAML file Yendell, Gary (DLSLtd, RAL, LSCI) via Tech-talk
Next: Re: PVI Prototype - a framework for specifying the interface to an EPICS driver in a single YAML file Yendell, Gary (DLSLtd, RAL, LSCI) via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  <20202021  2022  2023  2024 
Navigate by Thread:
Prev: Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Carsten Winkler via Tech-talk
Next: Re: BASE 7.0.x@Windows_64 - Channel Access Context in shared library - "ca_context_destroy bug" Carsten Winkler via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  <20202021  2022  2023  2024 
ANJ, 25 May 2020 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·