Repository: pytorch
Issue: c++ use pybind11 to import torch  free(): invalid pointer
Labels: module: cpp,triaged,module: pybind

## ❓ Questions and Help

I meet this problem when I use c++ to exec python code with pybind11.

### code

```c++
py::scoped_interpreter guard{};
try
{
     py::module torch = py::module::import("torch");
}
catch (std::exception &e)
{
    LOG(ERROR) << "import torch failed:" << e.what();
    return false;
}
return true;
```

### Environment

```
gcc          7.3.0
torch        1.8.1+cu111
torchvision  0.9.1
python       3.8.5

OS: CentOS 7
```

### errors

```
*** Error in `/home/bin/Match': free(): invalid pointer: 0x0000000002fbaaf8 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x82139)[0x7faf6b2e6139]
/home/bin/UAVPanoMatch[0x441125]
/home/bin/UAVPanoMatch(_ZN12UAVPanoMatch11MainProcessEv+0x18c)[0x429e9c]
/home/bin/UAVPanoMatch(main+0x719)[0x420f29]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7faf6b286505]
/home/bin/UAVPanoMatch[0x421c29]
======= Memory map: ========
00400000-004cc000 r-xp 00000000 fd:02 27661270                           /home/bin/UAVPanoMatch
006cc000-006cd000 r--p 000cc000 fd:02 27661270                           /home/bin/UAVPanoMatch
006cd000-006ce000 rw-p 000cd000 fd:02 27661270                           /home/bin/UAVPanoMatch
006ce000-006e0000 rw-p 00000000 00:00 0 
009dd000-033d6000 rw-p 00000000 00:00 0                                  [heap]
7fae64000000-7fae64021000 rw-p 00000000 00:00 0 
7fae64021000-7fae68000000 ---p 00000000 00:00 0 
7fae6b2f2000-7fae6b532000 rw-p 00000000 00:00 0 
7fae6b532000-7fae6b536000 r--p 00000000 fd:00 7077                       /root/anaconda3/lib/python3.8/lib-dynload/_asyncio.cpython-38-x86_64-linux-gnu.so
7fae6b536000-7fae6b53b000 r-xp 00004000 fd:00 7077                       /root/anaconda3/lib/python3.8/lib-dynload/_asyncio.cpython-38-x86_64-linux-gnu.so
7fae6b53b000-7fae6b53f000 r--p 00009000 fd:00 7077                       /root/anaconda3/lib/python3.8/lib-dynload/_asyncio.cpython-38-x86_64-linux-gnu.so
7fae6b53f000-7fae6b540000 r--p 0000c000 fd:00 7077                       /root/anaconda3/lib/python3.8/lib-dynload/_asyncio.cpython-38-x86_64-linux-gnu.so
7fae6b540000-7fae6b542000 rw-p 0000d000 fd:00 7077                       /root/anaconda3/lib/python3.8/lib-dynload/_asyncio.cpython-38-x86_64-linux-gnu.so
7fae6b542000-7fae6b545000 r--p 00000000 fd:00 7764                       /root/anaconda3/lib/python3.8/lib-dynload/unicodedata.cpython-38-x86_64-linux-gnu.so
7fae6b545000-7fae6b54b000 r-xp 00003000 fd:00 7764                       /root/anaconda3/lib/python3.8/lib-dynload/unicodedata.cpython-38-x86_64-linux-gnu.so
7fae6b54b000-7fae6b64d000 r--p 00009000 fd:00 7764                       /root/anaconda3/lib/python3.8/lib-dynload/unicodedata.cpython-38-x86_64-linux-gnu.so
7fae6b64d000-7fae6b64e000 r--p 0010a000 fd:00 7764                       /root/anaconda3/lib/python3.8/lib-dynload/unicodedata.cpython-38-x86_64-linux-gnu.so
7fae6b64e000-7fae6b64f000 rw-p 0010b000 fd:00 7764                       /root/anaconda3/lib/python3.8/lib-dynload/unicodedata.cpython-38-x86_64-linux-gnu.so
7fae6b64f000-7fae6b7d0000 rw-p 00000000 00:00 0 
7fae6b7d0000-7fae6b7ef000 r--p 00000000 fd:00 2908                       /root/anaconda3/lib/libssl.so.1.1
7fae6b7ef000-7fae6b839000 r-xp 0001f000 fd:00 2908                       /root/anaconda3/lib/libssl.so.1.1
7fae6b839000-7fae6b852000 r--p 00069000 fd:00 2908                       /root/anaconda3/lib/libssl.so.1.1
7fae6b852000-7fae6b853000 ---p 00082000 fd:00 2908                       /root/anaconda3/lib/libssl.so.1.1
7fae6b853000-7fae6b85c000 r--p 00082000 fd:00 2908                       /root/anaconda3/lib/libssl.so.1.1
7fae6b85c000-7fae6b860000 rw-p 0008b000 fd:00 2908                       /root/anaconda3/lib/libssl.so.1.1
7fae6b860000-7fae6b870000 r--p 00000000 fd:00 7399                       /root/anaconda3/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so
7fae6b870000-7fae6b879000 r-xp 00010000 fd:00 7399                       /root/anaconda3/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so
7fae6b879000-7fae6b885000 r--p 00019000 fd:00 7399                       /root/anaconda3/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so
7fae6b885000-7fae6b886000 ---p 00025000 fd:00 7399                       /root/anaconda3/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so
7fae6b886000-7fae6b887000 r--p 00025000 fd:00 7399                       /root/anaconda3/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so
7fae6b887000-7fae6b88e000 rw-p 00026000 fd:00 7399                       /root/anaconda3/lib/python3.8/lib-dynload/_ssl.cpython-38-x86_64-linux-gnu.so
7fae6b88e000-7fae6b9ce000 rw-p 00000000 00:00 0 
7fae6b9ce000-7fae6b9d0000 r--p 00000000 fd:00 6123                       /root/anaconda3/lib/python3.8/lib-dynload/_multiprocessing.cpython-38-x86_64-linux-gnu.so
7fae6b9d0000-7fae6b9d1000 r-xp 00002000 fd:00 6123                       /root/anaconda3/lib/python3.8/lib-dynload/_multiprocessing.cpython-38-x86_64-linux-gnu.so
7fae6b9d1000-7fae6b9d2000 r--p 00003000 fd:00 6123                       /root/anaconda3/lib/python3.8/lib-dynload/_multiprocessing.cpython-38-x86_64-linux-gnu.so
7fae6b9d2000-7fae6b9d3000 r--p 00003000 fd:00 6123                       /root/anaconda3/lib/python3.8/lib-dynload/_multiprocessing.cpython-38-x86_64-linux-gnu.so
7fae6b9d3000-7fae6b9d4000 rw-p 00004000 fd:00 6123                       /root/anaconda3/lib/python3.8/lib-dynload/_multiprocessing.cpython-38-x86_64-linux-gnu.so
7fae6b9d4000-7fae6b9d8000 r--p 00000000 fd:00 7209                       /root/anaconda3/lib/python3.8/lib-dynload/array.cpython-38-x86_64-linux-gnu.so
7fae6b9d8000-7fae6b9e1000 r-xp 00004000 fd:00 7209                       /root/anaconda3/lib/python3.8/lib-dynload/array.cpython-38-x86_64-linux-gnu.so
7fae6b9e1000-7fae6b9e6000 r--p 0000d000 fd:00 7209                       /root/anaconda3/lib/python3.8/lib-dynload/array.cpython-38-x86_64-linux-gnu.so
7fae6b9e6000-7fae6b9e7000 r--p 00011000 fd:00 7209                       /root/anaconda3/lib/python3.8/lib-dynload/array.cpython-38-x86_64-linux-gnu.sostart.sh: line 16: 36770 Aborted
```


cc @yf225 @glaringlee