Repository: pytorch
Issue: "munmap_chunk(): invalid pointer" interaction error with pytorch (< 1.10), pybind, and cv_bridge
Labels: triaged,module: lts

### 🐛 Describe the bug

I'm encountering a `munmap_chunk(): invalid pointer` error when using versions of `pytorch` <= 1.10. This bug appears to be the result of a complex interaction between several different libraries ([cv_bridge](http://wiki.ros.org/cv_bridge), [pybind11](https://pybind11.readthedocs.io/en/stable/), and `pytorch`), so I'm not entirely sure which package is the root cause. I've chosen to open this under `pytorch` because upgrading to `1.10` resolves the issue, but I'm happy to move this to the appropriate venue.

The minimum example required to reproduce is on this [public gist](https://gist.github.com/danielmohansahu/cec8ee27b3f191f65686838639b39b05). Note that this requires `docker` to run, but no other dependencies. This example uses `pybind` to [embed the Python interpreter](https://pybind11.readthedocs.io/en/stable/advanced/embedding.html) and import `torchvision`. It also links against `cv_bridge`. To reproduce download / clone and run the following:

```bash
# build image with dependencies
docker build . -t mwe

# drop into interactive container
docker run -it --rm mwe

# from within container
./mwe                                          # results in "munmap_chunk(): invalid pointer"
python3 -c "import torchvision.models"         # nominal import works fine
```

As you can see from [the Dockerfile](https://gist.github.com/danielmohansahu/cec8ee27b3f191f65686838639b39b05#file-dockerfile) we've tested multiple `pytorch` versions and it seems like 1.10 is where things start to work as expected.
```
# the following versions SUCCEED
# RUN pip install torch==1.10.2+cu111 torchvision==0.9.2+cu111 -f "https://download.pytorch.org/whl/lts/1.8/torch_lts.html"
# RUN pip install torch==1.10.0+cu111 torchvision==0.11.0+cu111 -f https://download.pytorch.org/whl/cu111/torch_stable.html
# RUN pip install torch torchvision

# these versions FAIL (torch < 1.10)
# RUN pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 -f https://download.pytorch.org/whl/torch_stable.html
# RUN pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html
# RUN pip install torch==1.8.2+cu111 torchvision==0.9.2+cu111 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html
# RUN pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
RUN pip3 install torch==1.9.1+cu111 torchvision==0.10.1+cu111 -f https://download.pytorch.org/whl/cu111/torch_stable.html
```

### Versions

Version information collected from within the Docker container mentioned above:

```bash
root@42568dcce83a:/mwe/build# python3 collect_env.py
Collecting environment information...
PyTorch version: 1.9.1+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.8.10 (default, Nov 26 2021, 20:14:08)  [GCC 9.3.0] (64-bit runtime)
Python platform: Linux-5.13.0-30-generic-x86_64-with-glibc2.29
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
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.17.4
[pip3] torch==1.9.1+cu111
[pip3] torchvision==0.10.1+cu111
[conda] Could not collect
```