In cpp, extract code block to meth cause sigsegv

Following is err code, it raise err in ort_session.Run

#include "gtest/gtest.h"
#include "onnx_helper.h"
#include "ndarray.h"
#include "opencv_helper.h"
#include "vector_helper.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>

class OnnxHelper2Test : public testing::Test {
protected:
//    static void SetUpTestCase() {
//    }
};

std::pair<std::vector<float>, Shape> handle_input_img(const std::string &input_img_path) {
    // Load the image using OpenCV
    cv::Mat input_mat = cv::imread(input_img_path);

    // Convert to float
    cv::cvtColor(input_mat, input_mat, cv::COLOR_BGR2RGB);
    input_mat.convertTo(input_mat, CV_32F);
    // Normalize

    input_mat /= 255.0;
    // Convert the preprocessed image to a vector of floats
    Ndarray<float> input_img = mat_to_ndarray(input_mat);

//    std::string r = xarr_to_str(xa);
    input_img.transpose({2, 0, 1});
//    const std::string &xa_str1 = xarr_to_str(xa);
//    std::cout << xa_str1 << std::endl;
    input_img.expand_dims(0);
//    const std::string &xa_str2 = xarr_to_str(xa);
//    std::cout << xa_str2 << std::endl;

    return {input_img.data, input_img.shape};
}

void handle_out_img(Ort::Value &output_tensor, const std::string &out_img_path) {
    // Post-process the output
    auto out_img = tensor_to_ndarray(output_tensor).get_sub(0);
    auto out_shape = out_img.shape;
//    float *data_ptr = const_cast<float *>(tensor_data);
//    cv::Mat out_img(out_shape[2], out_shape[3], CV_32FC3, data_ptr);

    // Transpose the image
    out_img.transpose({1, 2, 0});

    // Scale the image to the range [0, 255]
    out_img *= 255;
    out_img.clip(0, 255);
    std::vector<uint8_t> out_img_int_data(out_img.data.begin(), out_img.data.end());

    // Save the output image using OpenCV
    auto out_mat = cv::Mat(out_img.shape[0], out_img.shape[1], CV_8UC3,
                           out_img_int_data.data()); // CV_8UC3 means 8u uint8_t + channel 3

    cv::cvtColor(out_mat, out_mat, cv::COLOR_RGB2BGR);
    cv::imwrite(out_img_path, out_mat);
}

// Method to set up the ONNX Runtime session
Ort::Session newSess(const std::string &onnx_model_path) {
    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "onnxruntime");
    Ort::SessionOptions se_opts;
    OrtCUDAProviderOptions cudaProviderOpts;
    cudaProviderOpts.gpu_mem_limit = 2 * 1024 * 1024 * 1024;
    se_opts.AppendExecutionProvider_CUDA(cudaProviderOpts);
    se_opts.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
    se_opts.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
    Ort::Session ort_session(env, onnx_model_path.c_str(), se_opts);
    return ort_session;
}

int main(int argc, char **argv) {
    // Load the ONNX model
    auto d = "/home/roroco/Dropbox/cpp/cpp_lib/test/fix/onnx";
    std::string onnx_model_path = std::format("{}/RealESRGAN_x4plus_anime_6B.onnx", d);

//    // Set up the ONNX Runtime session
//    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "onnxruntime");
//    Ort::SessionOptions se_opts;
//    OrtCUDAProviderOptions cudaProviderOpts;
//    cudaProviderOpts.gpu_mem_limit = 2 * 1024 * 1024 * 1024;
//    se_opts.AppendExecutionProvider_CUDA(cudaProviderOpts);
//    se_opts.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
//    se_opts.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
//    Ort::Session ort_session(env, onnx_model_path.c_str(), se_opts);
    auto ort_session = newSess(onnx_model_path);

    Ort::AllocatorWithDefaultOptions allocator;
    std::string input_name = ort_session.GetInputNameAllocated(0, allocator).get();
    std::string out_name = ort_session.GetOutputNameAllocated(0, allocator).get();

    // Perform inference
    std::vector<const char *> input_names = {input_name.c_str()};
    std::vector<const char *> out_names = {out_name.c_str()};

    Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator,
                                                            OrtMemType::OrtMemTypeDefault);

    // Load and preprocess input image
    std::string input_img_path = std::format("{}/small.jpg", d);

    auto [input_tensor_vector, input_shape] = handle_input_img(input_img_path);
    // Create the input tensor
    Ort::Value input_tensor = Ort::Value::CreateTensor<float>(
            memoryInfo,
            input_tensor_vector.data(),
            input_tensor_vector.size(),
            input_shape.data(),
            input_shape.size());

    // Create a vector of input tensors
    std::vector<Ort::Value> input_tensors = {};
    input_tensors.push_back(std::move(input_tensor));
    std::vector<Ort::Value> out_tensors = ort_session.Run(Ort::RunOptions{nullptr}, input_names.data(),
                                                          input_tensors.data(), input_tensors.size(),
                                                          out_names.data(), out_names.size());

    // Handle the output image
    const std::string &out_path = std::format("{}/out.jpg", d);
    handle_out_img(out_tensors[0], out_path);
    std::cout << std::format("out to {}", out_path);
}

When I don’t use newSess and put all newSess code in main it work, following is correct code

#include "gtest/gtest.h"
#include "onnx_helper.h"
#include "ndarray.h"
#include "opencv_helper.h"
#include "vector_helper.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>

class OnnxHelper2Test : public testing::Test {
protected:
//    static void SetUpTestCase() {
//    }
};

std::pair<std::vector<float>, Shape> handle_input_img(const std::string &input_img_path) {
    // Load the image using OpenCV
    cv::Mat input_mat = cv::imread(input_img_path);

    // Convert to float
    cv::cvtColor(input_mat, input_mat, cv::COLOR_BGR2RGB);
    input_mat.convertTo(input_mat, CV_32F);
    // Normalize

    input_mat /= 255.0;
    // Convert the preprocessed image to a vector of floats
    Ndarray<float> input_img = mat_to_ndarray(input_mat);

//    std::string r = xarr_to_str(xa);
    input_img.transpose({2, 0, 1});
//    const std::string &xa_str1 = xarr_to_str(xa);
//    std::cout << xa_str1 << std::endl;
    input_img.expand_dims(0);
//    const std::string &xa_str2 = xarr_to_str(xa);
//    std::cout << xa_str2 << std::endl;

    return {input_img.data, input_img.shape};
}

void handle_out_img(Ort::Value &output_tensor, const std::string &out_img_path) {
    // Post-process the output
    auto out_img = tensor_to_ndarray(output_tensor).get_sub(0);
    auto out_shape = out_img.shape;
//    float *data_ptr = const_cast<float *>(tensor_data);
//    cv::Mat out_img(out_shape[2], out_shape[3], CV_32FC3, data_ptr);

    // Transpose the image
    out_img.transpose({1, 2, 0});

    // Scale the image to the range [0, 255]
    out_img *= 255;
    out_img.clip(0, 255);
    std::vector<uint8_t> out_img_int_data(out_img.data.begin(), out_img.data.end());

    // Save the output image using OpenCV
    auto out_mat = cv::Mat(out_img.shape[0], out_img.shape[1], CV_8UC3,
                           out_img_int_data.data()); // CV_8UC3 means 8u uint8_t + channel 3

    cv::cvtColor(out_mat, out_mat, cv::COLOR_RGB2BGR);
    cv::imwrite(out_img_path, out_mat);
}

// Method to set up the ONNX Runtime session
Ort::Session newSess(const std::string &onnx_model_path) {
    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "onnxruntime");
    Ort::SessionOptions se_opts;
    OrtCUDAProviderOptions cudaProviderOpts;
    cudaProviderOpts.gpu_mem_limit = 2 * 1024 * 1024 * 1024;
    se_opts.AppendExecutionProvider_CUDA(cudaProviderOpts);
    se_opts.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
    se_opts.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
    Ort::Session ort_session(env, onnx_model_path.c_str(), se_opts);
    return ort_session;
}

int main(int argc, char **argv) {
    // Load the ONNX model
    auto d = "/home/roroco/Dropbox/cpp/cpp_lib/test/fix/onnx";
    std::string onnx_model_path = std::format("{}/RealESRGAN_x4plus_anime_6B.onnx", d);

    // Set up the ONNX Runtime session
    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "onnxruntime");
    Ort::SessionOptions se_opts;
    OrtCUDAProviderOptions cudaProviderOpts;
    cudaProviderOpts.gpu_mem_limit = 2 * 1024 * 1024 * 1024;
    se_opts.AppendExecutionProvider_CUDA(cudaProviderOpts);
    se_opts.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
    se_opts.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL);
    Ort::Session ort_session(env, onnx_model_path.c_str(), se_opts);
//    auto ort_session = newSess(onnx_model_path);

    Ort::AllocatorWithDefaultOptions allocator;
    std::string input_name = ort_session.GetInputNameAllocated(0, allocator).get();
    std::string out_name = ort_session.GetOutputNameAllocated(0, allocator).get();

    // Perform inference
    std::vector<const char *> input_names = {input_name.c_str()};
    std::vector<const char *> out_names = {out_name.c_str()};

    Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator,
                                                            OrtMemType::OrtMemTypeDefault);

    // Load and preprocess input image
    std::string input_img_path = std::format("{}/small.jpg", d);

    auto [input_tensor_vector, input_shape] = handle_input_img(input_img_path);
    // Create the input tensor
    Ort::Value input_tensor = Ort::Value::CreateTensor<float>(
            memoryInfo,
            input_tensor_vector.data(),
            input_tensor_vector.size(),
            input_shape.data(),
            input_shape.size());

    // Create a vector of input tensors
    std::vector<Ort::Value> input_tensors = {};
    input_tensors.push_back(std::move(input_tensor));
    std::vector<Ort::Value> out_tensors = ort_session.Run(Ort::RunOptions{nullptr}, input_names.data(),
                                                          input_tensors.data(), input_tensors.size(),
                                                          out_names.data(), out_names.size());

    // Handle the output image
    const std::string &out_path = std::format("{}/out.jpg", d);
    handle_out_img(out_tensors[0], out_path);
    std::cout << std::format("out to {}", out_path);
}

I just confused why it’s wrong

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật