Skip to content

Commit dccfb1b

Browse files
Catching exceptions when failing to load shared library
1 parent 77f0aa2 commit dccfb1b

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

Src/runcpp2/runcpp2.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,23 @@ namespace
207207
}
208208

209209
//Load it
210-
dylib sharedLib(compiledSharedLibPath, dylib::no_filename_decorations);
210+
std::unique_ptr<dylib> sharedLib;
211+
212+
try
213+
{
214+
sharedLib = std::unique_ptr<dylib>(new dylib(compiledSharedLibPath, dylib::no_filename_decorations));
215+
}
216+
catch(std::exception& e)
217+
{
218+
ssLOG_ERROR("Failed to load shared library " << compiledSharedLibPath <<
219+
" with exception: ");
220+
221+
ssLOG_ERROR(e.what());
222+
return -1;
223+
}
211224

212225
//Get main as entry point
213-
if(sharedLib.has_symbol("main") == false)
226+
if(sharedLib->has_symbol("main") == false)
214227
{
215228
ssLOG_ERROR("The shared library does not have a main function");
216229
return -1;
@@ -221,7 +234,7 @@ namespace
221234

222235
try
223236
{
224-
scriptFullMain = sharedLib.get_function<int(int, char**)>("main");
237+
scriptFullMain = sharedLib->get_function<int(int, char**)>("main");
225238
}
226239
catch(const dylib::exception& ex)
227240
{
@@ -237,7 +250,7 @@ namespace
237250
{
238251
try
239252
{
240-
scriptMain = sharedLib.get_function<int()>("_main");
253+
scriptMain = sharedLib->get_function<int()>("_main");
241254
}
242255
catch(const dylib::exception& ex)
243256
{
@@ -258,22 +271,30 @@ namespace
258271

259272
//Run the entry point
260273
int result = 0;
261-
if(scriptFullMain != nullptr)
274+
try
262275
{
263-
std::vector<std::string> runArgsCopy = runArgs;
264-
runArgsCopy.insert(runArgsCopy.begin(), scriptPath);
265-
266-
std::vector<char*> runArgsCStr(runArgsCopy.size());
267-
for(int i = 0; i < runArgsCopy.size(); ++i)
268-
runArgsCStr[i] = &runArgsCopy[i][0];
269-
270-
result = scriptFullMain(runArgsCStr.size(), runArgsCStr.data());
276+
if(scriptFullMain != nullptr)
277+
{
278+
std::vector<std::string> runArgsCopy = runArgs;
279+
runArgsCopy.insert(runArgsCopy.begin(), scriptPath);
280+
281+
std::vector<char*> runArgsCStr(runArgsCopy.size());
282+
for(int i = 0; i < runArgsCopy.size(); ++i)
283+
runArgsCStr[i] = &runArgsCopy[i][0];
284+
285+
result = scriptFullMain(runArgsCStr.size(), runArgsCStr.data());
286+
}
287+
else if(scriptMain != nullptr)
288+
result = scriptMain();
289+
}
290+
catch(std::exception& e)
291+
{
292+
ssLOG_ERROR("Failed to run script main with exception: " << e.what());
293+
return -1;
271294
}
272-
else if(scriptMain != nullptr)
273-
result = scriptMain();
274295

275296
return result;
276-
297+
277298
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(-1);
278299
}
279300

0 commit comments

Comments
 (0)