Repository: pytorch
Issue: Segfault on unloading a model
Labels: oncall: jit,module: crash,module: cpp

### 🐛 Describe the bug

Hi,
I'm using pytorch compiled from source and with this very simple program which successfully loads a pre-trained model, then terminates, I get a segfault in the sequence of destructors as the model goes out of scope. The same thing happens if I use pre-compiled binaries.

Anyone seen this before?

Code

```
#include <iostream>
#include <torch/script.h> 
#include <memory>

int main(int argc, char **argv)
{

    try {
        std::cout << "Attempting to load " << argv[1] << "\n";
        // Deserialize the ScriptModule from a file using torch::jit::load().
        auto module = torch::jit::load(argv[1]);

        //this is output
        std::cout << "Model loaded\n";
    }
    catch (const c10::Error& e) {
        std::cerr << "Error loading Pytorch model " << e.msg() << "\n";
        return -1;
    }

    return 0;
}
```
Stack trace

#0 0x000055555555a27a in c10::IValue::isTensor (this=0x80) at /usr/local/libtorch/include/ATen/core/ivalue.h:394
https://github.com/pytorch/vision/pull/1 0x000055555555ac77 in c10::IValue::destroy (this=0x80) at /usr/local/libtorch/include/ATen/core/ivalue.h:991
https://github.com/pytorch/vision/issues/2 0x000055555555a24c in c10::IValue::~IValue (this=0x80, __in_chrg=) at /usr/local/libtorch/include/ATen/core/ivalue.h:197
https://github.com/pytorch/vision/pull/3 0x0000555555561419 in std::_Destroyc10::IValue (__pointer=0x80) at /usr/include/c++/9/bits/stl_construct.h:98
https://github.com/pytorch/vision/pull/4 0x00005555555604a2 in std::_Destroy_aux::__destroyc10::IValue* (__first=0x80, __last=0x21) at /usr/include/c++/9/bits/stl_construct.h:108
https://github.com/pytorch/vision/pull/5 0x000055555555f324 in std::_Destroyc10::IValue* (__first=0x80, __last=0x21) at /usr/include/c++/9/bits/stl_construct.h:137
https://github.com/pytorch/vision/pull/6 0x000055555555d79d in std::_Destroy<c10::IValue*, c10::IValue> (__first=0x80, __last=0x21) at /usr/include/c++/9/bits/stl_construct.h:206
https://github.com/pytorch/vision/pull/7 0x000055555555c151 in std::__cxx1998::vector<c10::IValue, std::allocatorc10::IValue >::~vector (this=0x555556b2e6d0, __in_chrg=) at /usr/include/c++/9/bits/stl_vector.h:677
https://github.com/pytorch/vision/pull/8 0x000055555555a220 in std::__debug::vector<c10::IValue, std::allocatorc10::IValue >::~vector (this=0x555556b2e6b8, __in_chrg=) at /usr/include/c++/9/debug/vector:220
https://github.com/pytorch/vision/issues/9 0x000055555555dbf6 in c10::ivalue::Object::~Object (this=0x555556b2e660, __in_chrg=) at /usr/local/libtorch/include/ATen/core/ivalue_inl.h:1328
https://github.com/pytorch/vision/pull/10 0x000055555555dc32 in c10::ivalue::Object::~Object (this=0x555556b2e660, _in_chrg=) at /usr/local/libtorch/include/ATen/core/ivalue_inl.h:1328
https://github.com/pytorch/vision/issues/11 0x000055555555dd6d in c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_typec10::ivalue::Object >::reset (this=0x7fffffffdcb0) at /usr/local/libtorch/include/c10/util/intrusive_ptr.h:289
https://github.com/pytorch/vision/pull/12 0x000055555555c37c in c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_typec10::ivalue::Object >::~intrusive_ptr (this=0x7fffffffdcb0, __in_chrg=) at /usr/local/libtorch/include/c10/util/intrusive_ptr.h:366
https://github.com/pytorch/vision/issues/13 0x000055555555b460 in torch::jit::Object::~Object (this=0x7fffffffdcb0, __in_chrg=) at /usr/local/libtorch/include/torch/csrc/jit/api/object.h:24
https://github.com/pytorch/vision/pull/14 0x000055555555b480 in torch::jit::Module::~Module (this=0x7fffffffdcb0, __in_chrg=) at /usr/local/libtorch/include/torch/csrc/jit/api/module.h:98
https://github.com/pytorch/vision/pull/15 0x00005555555589be in main (argc=2, argv=0x7fffffffddf8) at ./exe/hello_world.cpp:87

### Versions

PyTorch version: 1.3.1
Is debug build: False
CUDA used to build PyTorch: 10.0.130
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 9.0.0-2~ubuntu18.04.2 (tags/RELEASE_900/final)
CMake version: version 3.22.1
Libc version: glibc-2.10

Python version: 3.7.3 (default, Mar 27 2019, 22:11:17)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-4.15.0-128-generic-x86_64-with-debian-buster-sid
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.16.3
[pip3] torch==1.3.1
[conda] _pytorch_select           0.2                       gpu_0  
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.0.130                      0  
[conda] mkl                       2020.2                      256  
[conda] mkl-include               2022.0.1           h06a4308_117  
[conda] mkl-service               2.3.0            py37he8ac12f_0  
[conda] mkl_fft                   1.2.0            py37h23d657b_0  
[conda] mkl_random                1.1.1            py37h0573a6f_0  
[conda] numpy                     1.16.3           py37h7e9f1db_0  
[conda] numpy-base                1.16.3           py37hde5b4d6_0  
[conda] pytorch                   1.3.1           cuda100py37h53c1284_0

cc @jbschlosser