Skip to content

Session Run throws an access violation exception when I recreate the session #18578

Open
@xeeetu

Description

@xeeetu

Describe the issue

First I create variables which I then initialise. In the main function in the while loop, when a certain condition is met, I have to change the model for the output. I try to clear the variables initialised above and assign new values to them, but after changing their values the Session.Run function throws an exception: 0xC00000000005: Access violation reading location 0x0000000000000000000000000000000000000000000000000000.
I have a question, what am I doing wrong and how to change the values in the inference variables correctly. Do the variables Env, RunOptions, MemoryInfo need to be created again each time the model is updated or should they be initialised once when the program is started? Sorry for code repetition, I tried to put initialisation in a separate function, passing variables by value, but I get the same address access error (is it possible to pass Ort variables by reference? because they contain smart pointers inside them and ownership is lost? ).

To reproduce

int main()
{
const int _netWidth = INPUT_W; //ONNX-net-input-width
const int _netHeight = INPUT_H; //ONNX-net-input-height

    int _batchSize = 1;  //if multi-batch,set this

    //ONNXRUNTIME	
    Ort::Env _OrtEnv = Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_ERROR, "YOLOV5");
    Ort::SessionOptions _OrtSessionOptions = Ort::SessionOptions();
    Ort::Session* _OrtSession = nullptr;
    auto _OrtMemoryInfo(Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtDeviceAllocator,   OrtMemType::OrtMemTypeCPUOutput));
    Ort::RunOptions _RunOptions{ nullptr };

#if ORT_API_VERSION < ORT_OLD_VISON
char* _inputName, * _output_name0;
#else
std::shared_ptr _inputName, _output_name0;
#endif

    std::vector<char*> _inputNodeNames; 
    std::vector<char*> _outputNodeNames;

    size_t _inputNodesNum = 0;  
    size_t _outputNodesNum = 0;  

    ONNXTensorElementDataType _inputNodeDataType; 
    ONNXTensorElementDataType _outputNodeDataType;
    std::vector<int64_t> _inputTensorShape; 

    std::vector<int64_t> _outputTensorShape;


    //-------------------------InitDetector----------------------------

    _OrtSessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);

#ifdef _WIN32
std::wstring model_path(modelPath.begin(), modelPath.end());
_OrtSession = new Ort::Session(_OrtEnv, model_path.c_str(), _OrtSessionOptions);
#else
_OrtSession = new Ort::Session(_OrtEnv, modelPath.c_str(), _OrtSessionOptions);
#endif

    Ort::AllocatorWithDefaultOptions allocator;

    //init input
    _inputNodesNum = _OrtSession->GetInputCount();

#if ORT_API_VERSION < ORT_OLD_VISON
_inputName = _OrtSession->GetInputName(0, allocator);
_inputNodeNames.push_back(_inputName);
#else
_inputName = std::move(_OrtSession->GetInputNameAllocated(0, allocator));
_inputNodeNames.push_back(_inputName.get());
#endif

    //cout << _inputNodeNames[0] << endl;
    Ort::TypeInfo inputTypeInfo = _OrtSession->GetInputTypeInfo(0);
    auto input_tensor_info = inputTypeInfo.GetTensorTypeAndShapeInfo();
    _inputNodeDataType = input_tensor_info.GetElementType();
    _inputTensorShape = input_tensor_info.GetShape();

    //init output
    _outputNodesNum = _OrtSession->GetOutputCount();

#if ORT_API_VERSION < ORT_OLD_VISON
_output_name0 = _OrtSession->GetOutputName(0, allocator);
_outputNodeNames.push_back(_output_name0);
#else
_output_name0 = std::move(_OrtSession->GetOutputNameAllocated(0, allocator));
_outputNodeNames.push_back(_output_name0.get());

#endif
Ort::TypeInfo type_info_output0(nullptr);
type_info_output0 = _OrtSession->GetOutputTypeInfo(0); //output0

    auto tensor_info_output0 = type_info_output0.GetTensorTypeAndShapeInfo();
    _outputNodeDataType = tensor_info_output0.GetElementType();
    _outputTensorShape = tensor_info_output0.GetShape();

.
.
.
while(!(GetAsyncKeyState(0x70) < 0)
{
.
.
.
input_tensors.push_back(Ort::Value::CreateTensor(_OrtMemoryInfo, inputTensorValues.data(), input_tensor_length, _inputTensorShape.data(), _inputTensorShape.size()));

        output_tensors = _OrtSession->Run(_RunOptions,
            _inputNodeNames.data(),
            input_tensors.data(),
            _inputNodeNames.size(),
            _outputNodeNames.data(),
            _outputNodeNames.size()
        );

.
.
.
if(----condition----)
{
_OrtEnv.release();
_OrtSessionOptions.release();
if (*_OrtSession != nullptr)
delete _OrtSession;
_inputNodesNum = 0;
_inputName.reset();
_inputNodeNames.clear();
_inputTensorShape.clear();
_outputNodesNum = 0;
_output_name0.reset();
_outputNodeNames.clear();
_outputTensorShape.clear();

    //-------------------------InitDetector----------------------------
        _OrtSessionOptions = Ort::SessionOptions();

        _OrtSessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);

         modelPath = ....

#ifdef _WIN32
std::wstring model_path(modelPath.begin(), modelPath.end());
_OrtSession = new Ort::Session(_OrtEnv, model_path.c_str(), _OrtSessionOptions);
#else
_OrtSession = new Ort::Session(_OrtEnv, modelPath.c_str(), _OrtSessionOptions);
#endif

        Ort::AllocatorWithDefaultOptions allocator;

            //init input
            _inputNodesNum = _OrtSession->GetInputCount();

#if ORT_API_VERSION < ORT_OLD_VISON
_inputName = _OrtSession->GetInputName(0, allocator);
_inputNodeNames.push_back(_inputName);
#else
_inputName = std::move(_OrtSession->GetInputNameAllocated(0, allocator));
_inputNodeNames.push_back(_inputName.get());
#endif

            //cout << _inputNodeNames[0] << endl;
            Ort::TypeInfo inputTypeInfo = _OrtSession->GetInputTypeInfo(0);
            auto input_tensor_info = inputTypeInfo.GetTensorTypeAndShapeInfo();
            _inputNodeDataType = input_tensor_info.GetElementType();
            _inputTensorShape = input_tensor_info.GetShape();

            //init output
            _outputNodesNum = _OrtSession->GetOutputCount();

#if ORT_API_VERSION < ORT_OLD_VISON
_output_name0 = _OrtSession->GetOutputName(0, allocator);
_outputNodeNames.push_back(_output_name0);
#else
_output_name0 = std::move(_OrtSession->GetOutputNameAllocated(0, allocator));
_outputNodeNames.push_back(_output_name0.get());

#endif
Ort::TypeInfo type_info_output0(nullptr);
type_info_output0 = _OrtSession->GetOutputTypeInfo(0); //output0

            auto tensor_info_output0 = type_info_output0.GetTensorTypeAndShapeInfo();
            _outputNodeDataType = tensor_info_output0.GetElementType();
            _outputTensorShape = tensor_info_output0.GetShape();

}
.
.
.
}
.
.
.
return 0;
}

Urgency

As soon as possible

Platform

Windows

OS Version

10 22h2

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

1.16.0

ONNX Runtime API

C++

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiissues related to all other APIs: C, C++, Python, etc.platform:windowsissues related to the Windows platform

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions