[zeromq-dev] Static zmq context in Windows DLL
Osiris Pedroso
opedroso at gmail.com
Mon Dec 9 19:32:39 CET 2019
Sorry, my previous email was sent without much time (I had an online class
to attend...).
The idea is to define a single instance that holds your context.
When instantiating this singleton, make sure there is nothing else in the
source file (the singleton instantiation is the last executable statement
in the source file).
Then, in the line above this source line, add this pragma:
#pragman init_seg(lib)
MyClassHasAContext myContext;
//... no more code after it ...
The explanation for this pragma can be found here:
https://docs.microsoft.com/en-us/cpp/preprocessor/init-seg?view=vs-2019
Bottom line, all variables are normally defined with the default #pragma
init_seg(compiler).
All variables defines in the init_seg "lib" will be initialized (have their
constructors executed or their initializer functions executed) before any
functions from the init_seg "compiler".
This also works in reverse order when the DLL is shutting down. The classes
instantiated in the segment "lib" will have their destructors executed
after all static instances defined in the segment "compiler" have been
destroyed.
Obviously you can have more definitions after the pragma, but when defining
timing dependent code like this, it is better to separate the critical code
to a separate source file and make sure that only the items you need the
strict lifetime control are there.
Obviously the lifetime of object will be in the order they are defined in
the source file. If this is the only file with the init_seg "lib", the
first defined variable will have the all encompassing lifetime, the next
line, one bit less, and so on (for class instances). If you have more than
one source file with this pragma, it becomes more difficult to control
their lifetimes unless you control the linking order of the objects
generated. I would avoid that.
I also suggest you enable ... Native ... under Debug Options. When you set
a break in the debugger in your constructor, if you loaded the source code
for the C++ runtime (during DevStudio installation), you will be able to
see function name of the C++ runtime calling your constructor, find it in
the C++ runtime source code and understand what it is doing.
Hope it helps,
Osiris
On Mon, Dec 9, 2019 at 2:18 PM Attila Magyari <atti86 at gmail.com> wrote:
> Hello,
>
> I've encountered this issue: https://github.com/zeromq/czmq/issues/1788
> Basically when exiting the application, I get this error: *Assertion
> failed: Successful WSASTARTUP not yet performed.*
>
> I am using ZeroMQ on windows inside a DLL. I have a global static zeromq
> context, because I want it persistent across multiple instances of my
> objects, and there is no central place I could pass it from otherwise.
>
> The proposed solution is to call zsys_shutdown() before exiting the
> application, but I don't know how and where:
>
> - This function is not available through the API that I am using
> (zmq.h/zmq.hpp)
> - Not sure where would I call it, as I don't know when the application
> exits. In my library there is no main object that could control the
> lifetime of the zmq layer
>
> Any idea how to approach this?
>
> Kinds regards,
> Attila
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20191209/a86bc056/attachment.htm>
More information about the zeromq-dev
mailing list