The following snippet of code which converts an image into a tensor, results in access violation exception when run in debug mode only.
auto ToTensor(cv::Mat img, bool show_output = false, bool unsqueeze = false, int unsqueeze_dim = 0)
{
    try
    {
        if (show_output)
            std::cout << "image shape: " << img.size() << " channels: " << img.channels() << " depth: " << img.depth() << " type: " << img.type() << std::endl;
        at::Tensor tensor_image = torch::from_blob(img.data, { img.rows, img.cols, img.channels() }, at::kByte);

        if (unsqueeze)
        {
            tensor_image.unsqueeze_(unsqueeze_dim);
            std::cout << "tensors new shape: " << tensor_image.sizes() << std::endl;
        }

        if (show_output)
        {
            std::cout << tensor_image.slice(2, 0, 1) << std::endl;
        }
        
        if (show_output)
            std::cout << "tenor shape: " << tensor_image.sizes() << std::endl;
        
        return tensor_image;
    }
    catch (const c10::Error& e)
    {
        std::cout << "error occured: " << e.msg() << e.what() << std::endl;
        return torch::tensor({ 0.229, 0.224, 0.225 });
    }
}
And here is  the full exception message :  

exception message :
Exception thrown at 0x00007FF70568DB3A in test.exe: 0xC0000005: Access violation writing location 0x0000000000000003.

Whats wrong here?