Hi Everyone,

We were able to clear the memory leaks reported in Visual Studio Debug 
builds by moving away from the Singletons and using file scope class 
objects. The limitation is we need to use a version of MSVC that supports 
init_seg(), which is VS2002 and above. We are in the green zone since we 
dropped VC++ 5.0 and 6.0 support.

The former code looked something like:

  const Integer& s_zero = Singleton<Integer, NewInteger<0L> >().Ref();
  const Integer& Zero() {
    return s_zero;
  }

The new code looks like:

  #pragma init_seg(lib)
  const InitializeInteger s_init;
  const CryptoPP::Integer s_zero(0L);
  #pragma warning(default: 4073)
  const Integer& Zero() {
    return s_zero;
  }

The init_seg() ensures our objects are in the CRT static initializer list, 
so we don't suffer Microsoft's "magic static" deficiencies. Magic statics 
are just dynamic initializers. They are a core C++11 language feature. 
Clang had them at 2.9 and GCC had them at 4.3. Visual Studio 2015 was the 
first to have them. Its not clear why Microsoft waited a decade to provide 
them.

We control the order of intialization by ordering our source and object 
files. The linker is deterministic, and assigns section names in an 
increasing order based on the way object files are fed to it. When they are 
combined, we are assured cryptlib objects are created before cpu objects, 
cpu objects are created before integer objects, etc.

By using the CRT static initializer list, we also side step all the 
threading problems related to initialization. The CRT intializers are run 
at program startup under a single thread.

You can read more about Microsoft's CRT initalization function (initterm) 
and startup code at OSDev wiki 
(http://wiki.osdev.org/Visual_C%2B%2B_Runtime).

The check-in of interest was 
https://github.com/weidai11/cryptopp/commit/301437e693fe8bff.

Jeff

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to cryptopp-users-unsubscr...@googlegroups.com.
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to