Repository: pytorch
Issue: [C++] Libtorch error in Release build only 
Labels: module: abi,triaged

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
Hi!
The following program works fine using Debug build. But on Release build it gives the runtime error:

```
Unhandled exception thrown: read access violation:
_Right was 0x10.
```

I tried to compile the program with different compiler options (using Microsoft Visual C++ compiler). It works with /MDd option (debug run-time library), and fails with /MD option (release run-time library)

What can be the reason for that?
Here is full program code:

```
#include <torch/torch.h>
#include <stdio.h>

int main() {
    std::cout << "Program started";
    torch::nn::Sequential seq(torch::nn::Linear(1, 1));
    std::cout << "Program finished";
}
```
Stacktrace:

```
>	MyChessCpp.exe!std::operator==<char,std::char_traits<char>,std::allocator<char>>(const std::string & _Left, const std::string & _Right)Line 4378	C++
 	[Embedded frame] MyChessCpp.exe!std::equal_to<std::string>::operator()(const std::string &)Line 91	C++
 	[Embedded frame] MyChessCpp.exe!std::_Uhash_compare<std::string,std::hash<std::string>,std::equal_to<std::string>>::operator()(const std::string &)Line 164	C++
 	[Embedded frame] MyChessCpp.exe!std::_Hash<std::_Umap_traits<std::string,unsigned __int64,std::_Uhash_compare<std::string,std::hash<std::string>,std::equal_to<std::string>>,std::allocator<std::pair<std::string const ,unsigned __int64>>,0>>::_Find_last(const std::string &)Line 1677	C++
 	[Embedded frame] MyChessCpp.exe!std::_Hash<std::_Umap_traits<std::string,unsigned __int64,std::_Uhash_compare<std::string,std::hash<std::string>,std::equal_to<std::string>>,std::allocator<std::pair<std::string const ,unsigned __int64>>,0>>::count(const std::string &)Line 1360	C++
 	MyChessCpp.exe!torch::OrderedDict<std::string,std::shared_ptr<torch::nn::Module>>::insert<std::string,std::shared_ptr<torch::nn::Module>>(std::string && key, std::shared_ptr<torch::nn::Module> && value)Line 363	C++
 	MyChessCpp.exe!torch::OrderedDict<std::string,std::shared_ptr<torch::nn::Module>>::insert(std::string key, std::shared_ptr<torch::nn::Module> && value)Line 373	C++
 	MyChessCpp.exe!torch::nn::Module::register_module<torch::nn::Module>(std::string name, std::shared_ptr<torch::nn::Module> module)Line 649	C++
 	MyChessCpp.exe!torch::nn::SequentialImpl::push_back(std::string name, torch::nn::AnyModule any_module)Line 255	C++
 	[Embedded frame] MyChessCpp.exe!torch::nn::SequentialImpl::push_back(std::string)Line 203	C++
 	MyChessCpp.exe!torch::nn::SequentialImpl::push_back<torch::nn::LinearImpl>(std::string name, const torch::nn::ModuleHolder<torch::nn::LinearImpl> & module_holder)Line 236	C++
 	[Embedded frame] MyChessCpp.exe!torch::nn::SequentialImpl::push_back(const torch::nn::ModuleHolder<torch::nn::LinearImpl> &)Line 229	C++
 	[Embedded frame] MyChessCpp.exe!torch::nn::SequentialImpl::{ctor}(torch::nn::Linear &&)Line 103	C++
 	[Embedded frame] MyChessCpp.exe!torch::nn::ModuleHolder<torch::nn::SequentialImpl>::{ctor}(torch::nn::Linear &&)Line 64	C++
 	MyChessCpp.exe!torch::nn::Sequential::Sequential<torch::nn::Linear,void>(torch::nn::Linear && __param0)Line 8	C++
 	MyChessCpp.exe!main()Line 6	C++
 	[Embedded frame] MyChessCpp.exe!invoke_main()Line 78	C++
 	MyChessCpp.exe!__scrt_common_main_seh()Line 288	C++
 	kernel32.dll!00007ffd583e7bd4()	No data
 	ntdll.dll!00007ffd5a38ced1()	No data	
```


## To Reproduce

Steps to reproduce the behavior:

1. Download Libtorch for Windows, C++, CUDA 10.2 from here: https://pytorch.org/get-started/locally/
2. Setup environment
3. Make project using CMake
4. Debug version:
    a. Use the code above to compile project using Debug build - build must be successful
    b. Run debug version in console - there must be no errors.  "Program startedProgram finished" must be printed.
5. Release version in console:
    a. Use the code above to compile project using Release build - build must be successful
    b. Run release version in console - there must be no errors.  
    **Expected:** "Program startedProgram finished" printed. 
    **Actually:** "Program started" printed. 
6. Release version in debugger:
    a. Run **release** version under Microsoft Visual Studio (under debugger). 
    **Expected:** "Program startedProgram finished" printed. No errors occured.
    **Actually:** "Program started" printed. **Got exception on Line 6**


## Environment
Collecting environment information...
PyTorch version: 1.5.1
Is debug build: No
CUDA used to build PyTorch: 10.2

OS: Microsoft Windows 10 Pro
GCC version: Could not collect
CMake version: version 3.18.0

Python version: 3.8
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\cudnn_ops_train64_8.dll

Versions of relevant libraries:
[pip3] numpy==1.18.4
[pip3] torch==1.5.1
[pip3] torchvision==0.6.1
[conda] Could not collect