Restore state feature in NVENC samples

Hi,
We are using Video Codec SDK 12.1 for HEVC encoding. Now we are trying to adopt the new “NvEncRestoreEncoderState” function and having to struggle with it. We encode one picture 4 times with different qp settings and select only one we need (advancing the state for it)
We modified sample’s NvEncoder::CreateDefaultEncoderParams with

pIntializeParams->numStateBuffers = 4

added new function

void NvEncoderCustom::RestoreState(int32_t index)
{
NV_ENC_RESTORE_ENCODER_STATE_PARAMS param{};
param.version = NV_ENC_RESTORE_ENCODER_STATE_PARAMS_VER;
param.bufferIdx = index;
param.state = NV_ENC_STATE_RESTORE_FULL;
NVENC_API_CALL(m_nvenc.nvEncRestoreEncoderState(m_hEncoder, &param));
}

and modified NvEncoder::DoEncode()

picParams.frameIdx = current_pic_number;`

So, after 4 calls EncodeFrame() we call RestoreState(chosen_index) with selected index.
For unknown reason, it works for all presets on Windows platform, but not all presets on Linux. For presets P1 and P2 we catch a hang up at NVENC_API_CALL(m_nvenc.nvEncLockBitstream(m_hEncoder, &lockBitstreamData)) in NvEncoder::GetEncodedPacket().

The question is, will there an example for restore feature usage in SDK samples?