From 7ef6bec04c7fc7051afe27ce19318471802f4aa3 Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Mon, 6 Dec 2010 18:28:30 +0300 Subject: [PATCH] Unload driver on gen_server termination --- src/sqlite3.erl | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/sqlite3.erl b/src/sqlite3.erl index 4f0150e..e7a52ac 100644 --- a/src/sqlite3.erl +++ b/src/sqlite3.erl @@ -540,7 +540,8 @@ init(Options) -> Port = open_port({spawn, create_port_cmd(DbFile)}, [binary]), {ok, #state{port = Port, ops = Options}}; {error, Error} -> - Msg = io_lib:format("Error loading ~p: ~s", [?DRIVER_NAME, erl_ddll:format_error(Error)]), + Msg = io_lib:format("Error loading ~p: ~s", + [?DRIVER_NAME, erl_ddll:format_error(Error)]), {stop, lists:flatten(Msg)} end. @@ -662,7 +663,7 @@ handle_info(_Info, State) -> {noreply, State}. %%-------------------------------------------------------------------- -%% @spec terminate(Reason, State) -> void() +%% @spec terminate(Reason, State) -> term() %% @doc This function is called by a gen_server when it is about to %% terminate. It should be the opposite of Module:init/1 and do any necessary %% cleaning up. When it returns, the gen_server terminates with Reason. @@ -670,13 +671,22 @@ handle_info(_Info, State) -> %% @end %% @hidden %%-------------------------------------------------------------------- --spec terminate(atom(), tuple()) -> atom(). -terminate(normal, #state{port = Port}) -> - port_command(Port, term_to_binary({close, nop})), - port_close(Port), - ok; -terminate(_Reason, _State) -> - ok. +-spec terminate(atom(), tuple()) -> term(). +terminate(_Reason, #state{port = Port}) -> + case Port of + undefined -> + pass; + _ -> + port_command(Port, term_to_binary({close, nop})), + port_close(Port) + end, + case erl_ddll:unload(?DRIVER_NAME) of + ok -> + ok; + {error, ErrorDesc} -> + error_logger:error_msg("Error unloading sqlite3 driver: ~s~n", + [erl_ddll:format_error(ErrorDesc)]) + end. %%-------------------------------------------------------------------- %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}