Repository: pytorch
Issue: The "register_module" function got a "key_description=<error>" and reported that "Exception 0xc0000005 encountered at address 0x7ff7fb3dfbf3: Access violation reading location 0xffffffffffffffff"
Labels: module: windows,module: cpp,triaged

This is my code.The program was crashed in line 
  conv = register_module("conv", torch::nn::Conv2d(torch::nn::Conv2dOptions(in_channels, out_channels, kernel_size).stride(stride).padding(kernel_size / 2)));

```cpp
#include <torch/torch.h>
#include <iostream>

// 定义卷积 + ReLU + 批量归一化的模块
struct ConvReluBn : torch::nn::Module {
    ConvReluBn(int in_channels, int out_channels, int kernel_size, int stride = 1) {
        conv = register_module("conv", torch::nn::Conv2d(torch::nn::Conv2dOptions(in_channels, out_channels, kernel_size).stride(stride).padding(kernel_size / 2)));
        relu = register_module("relu", torch::nn::ReLU());
        bn = register_module("bn", torch::nn::BatchNorm2d(out_channels));
    }

    torch::Tensor forward(torch::Tensor x) {
        return bn->forward(relu->forward(conv->forward(x)));
    }

private:
    torch::nn::Conv2d conv{nullptr};
    torch::nn::ReLU relu{nullptr};
    torch::nn::BatchNorm2d bn{nullptr};
};

// 定义 plainCNN 类
class plainCNN : public torch::nn::Module {
public:
    plainCNN(int in_channels, int out_channels);
    torch::Tensor forward(torch::Tensor x);

private:
    int mid_channels[3] = {32, 64, 128};
    // 使用 std::shared_ptr 来存储 ConvReluBn 对象
    std::shared_ptr<ConvReluBn> conv1;
    std::shared_ptr<ConvReluBn> down1;
    std::shared_ptr<ConvReluBn> conv2;
    std::shared_ptr<ConvReluBn> down2;
    std::shared_ptr<ConvReluBn> conv3;
    std::shared_ptr<ConvReluBn> down3;
    torch::nn::Conv2d out_conv{nullptr};
};

// 构造函数的实现
plainCNN::plainCNN(int in_channels, int out_channels) {
    // 使用 std::make_shared 创建 ConvReluBn 实例
    conv1 = std::make_shared<ConvReluBn>(in_channels, mid_channels[0], 3);
    down1 = std::make_shared<ConvReluBn>(mid_channels[0], mid_channels[0], 3, 2);
    conv2 = std::make_shared<ConvReluBn>(mid_channels[0], mid_channels[1], 3);
    down2 = std::make_shared<ConvReluBn>(mid_channels[1], mid_channels[1], 3, 2);
    conv3 = std::make_shared<ConvReluBn>(mid_channels[1], mid_channels[2], 3);
    down3 = std::make_shared<ConvReluBn>(mid_channels[2], mid_channels[2], 3, 2);
    out_conv = register_module("out_conv", torch::nn::Conv2d(torch::nn::Conv2dOptions(mid_channels[2], out_channels, 3).padding(1)));

    // 注册模块
    register_module("conv1", conv1);
    register_module("down1", down1);
    register_module("conv2", conv2);
    register_module("down2", down2);
    register_module("conv3", conv3);
    register_module("down3", down3);
}

// forward 方法的实现
torch::Tensor plainCNN::forward(torch::Tensor x) {
    x = conv1->forward(x);
    x = down1->forward(x);
    x = conv2->forward(x);
    x = down2->forward(x);
    x = conv3->forward(x);
    x = down3->forward(x);
    x = out_conv->forward(x);
    return x;
}

int main() {
    // 创建模型实例
    plainCNN model(3, 10);  // 输入通道3（RGB图像），输出通道10
    model.train(); // 设置模型为训练模式

    // 创建一个示例输入
    torch::Tensor input = torch::randn({1, 3, 224, 224}); // Batch size 1，3个通道，224x224的图像
    auto output = model.forward(input); // 前向传播

    std::cout << "Output shape: " << output.sizes() << std::endl; // 输出结果形状

    return 0;
}
```
my enviroment is windows x64 with CUDA12.4,I tested the script below to verify that I have prepared a suitable enviroment.And that runs well
```cpp
#include <iostream>
#include "torch/script.h"
#include "torch/torch.h"
using namespace std;

int main() {
    // 检查是否有可用的 GPU
    if (torch::cuda::is_available()) {
        cout << "CUDA is available! Using GPU." << endl;
        torch::Device device(torch::kCUDA);
        torch::Tensor tmp_1 = torch::rand({2, 3}).to(device);
        torch::Tensor tmp_2 = torch::full_like(tmp_1, 1).to(device);

        cout << tmp_1 << endl;
        cout << tmp_2 << endl;
    } else {
        cout << "CUDA is not available. Using CPU." << endl;
        torch::Device device(torch::kCPU);
        torch::Tensor tmp_1 = torch::rand({2, 3}).to(device);
        torch::Tensor tmp_2 = torch::full_like(tmp_1, 1).to(device);

        cout << tmp_1 << endl;
        cout << tmp_2 << endl;
    }

    return 0;
}
```
And the build Type is DEBUG.Here is my CMAKELISTS
~~~CMAKE
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(custom_ops)

# 设置 CMAKE_PREFIX_PATH
set(CMAKE_PREFIX_PATH "D:/MyLibs/libtorch-win-shared-with-deps-2.5.0+cu124/libtorch")
find_package(Torch REQUIRED)

# 设置 C++17 标准
set(CMAKE_CXX_STANDARD 17)

add_executable(main
        src/simpleModel.cpp)

# 链接 libtorch 库
target_link_libraries(main "${TORCH_LIBRARIES}")
~~~

cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @iremyux @Blackhex @jbschlosser