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

<libTRT.cpp>

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

<runTRT.cpp>

#include "libTRT.h"
main(){
void * net_handle=foo();
 ..do some openCV image read..
 <b>bar((trtNet*)net_handle,readImg);</b>
}

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”);

main(){

…do some openCV image read…
bar(&net,readImg);
}[/code]

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?