How can I separately call engine deserialization and doInference for exporting tensorRT as a library?

Originally tensorRT engine init(deserialization) and doInference(enqueue) parts are in one function.
But if I want to call the init part only one time and then call doInference part several times for different input images,
I need to separate those parts.
First I made a manager class(called “trtNet”) and I put the engine init part into the class’s constructor.
and put all other parts into the class’s doInference function.

So, I thought I can create the trtNet instance like


#include "libTRT.h"
trtNet* foo(){
<b>trtNet net("myEningFile.engine");</b>
void bar(trtNet* net,cv::Mat img){


#include "libTRT.h"
void * net_handle=foo(); some openCV image read..

and I saw the returned net_handle value is “NULL” !! even thogh it had some pointer address value in the foo function(that’s before return, it is valid but after return, the pointer value is gone!!)

So, I modified it to declare global variable,

trtNet net(“myEngine.engine”);


…do some openCV image read…

it shows “seg fault” error while engine deserialization which runs OK in the previous cases.

Can anyone give me any hint for passing trtNet instance between functions from separate source code files?