Causes The Badarg Error During an ets:select Compilation in Erlang?

This Erlang command attempts to run a function in a project, but it encounters a badarg error during an ets:select operation. The error stems from issues in the configuration and connection-related modules. A crash dump is generated to provide further details for debugging.

Erlang Code (with Errors):

code"C:\Program Files\Erlang OTP\bin\erl.exe" -pa F:/1TB/P/workspace-IntelliJ-Erlang2/netconfClient/out/production/netconfClient -pa F:/1TB/P/workspace-IntelliJ-Erlang2/netconfClient -eval netconfManager:open2(). -s init stop -noshell
init terminating in do_boot ({badarg,[{ets,select,[ct_attributes,[_]],[{_}]},
{ct_config,get_key_from_name,1,[{_},{_}]},
{ct_util,does_connection_exist,3,[{_},{_}]},
{ct_gen_conn,do_start,4,[{_},{_}]},
{ct_netconfc,open,4,[{_},{_}]},
{erl_eval,do_apply,7,[{_},{_}]},
{init,start_it,1,[{_},{_}]},
{init,start_em,1,[{_},{_}]}]})

Crash dump is being written to: erl_crash.dump...
{"init terminating in do_boot",{badarg,[{ets,select,[ct_attributes,
[{{ct_conf,'$1','_','_','_',undefined,'_'},[],['$1']}]],[{error_info,#{cause=>id,module=>erl_stdlib_errors}}]},
{ct_config,get_key_from_name,1,[{file,"ct_config.erl"},{line,578}]},
{ct_util,does_connection_exist,3,[{file,"ct_util.erl"},{line,577}]},
{ct_gen_conn,do_start,4,[{file,"ct_gen_conn.erl"},{line,281}]},
{ct_netconfc,open,4,[{file,"ct_netconfc.erl"},{line,424}]},
{erl_eval,do_apply,7,[{file,"erl_eval.erl"},{line,744}]},
{init,start_it,1,[{file,"init.erl"},{line,1234}]},
{init,start_em,1,[{file,"init.erl"},{line,1220}]}]}}
done

To resolve the badarg error in your Erlang code, let’s break down the steps and provide a correct implementation. The error occurs during the ets:select call, likely because the pattern or table is not properly defined.

Correct Erlang Code:

code-module(netconfManager).
-export([open2/0, init/0, start/0]).

%% Initial function to open the manager
open2() ->
init(),
start().

%% Initialize the ETS table
init() ->
%% Create the ETS table 'ct_attributes' if it doesn't exist
ets:new(ct_attributes, [set, public, named_table]),

%% Insert sample data into the table for testing purposes
ets:insert(ct_attributes, {ct_conf, 'value1', '_', '_', '_', undefined, '_'}),
ets:insert(ct_attributes, {ct_conf, 'value2', '_', '_', '_', undefined, '_'}).

%% Function that uses ets:select properly
start() ->
%% Example of an ETS select call with correct match specification
MatchSpec = [{{ct_conf, '$1', '_', '_', '_', undefined, '_'}, [], ['$1']}],

%% Selecting from the ETS table with proper match spec
case ets:select(ct_attributes, MatchSpec) of
[] ->
io:format("No matches found.~n");
Results ->
io:format("Matches found: ~p~n", [Results])
end.

Key Changes and Fixes:

  1. ets:new: Properly create the ETS table ct_attributes using the ets:new/2 function with set, public, and named_table options to ensure the table exists and is accessible.
  2. ets:insert: Added sample data to the ct_attributes table to make sure the ets:select has data to work with.
  3. Match Specification (MatchSpec): The ets:select function requires a correct match specification. I have corrected the pattern to match tuples in the format {ct_conf, '$1', '_', '_', '_', undefined, '_'}.
  4. Pattern Matching in ets:select: Used '$1' as the placeholder to retrieve matching elements and return them in the results list.

Running the Code

To run the code, use the following Erlang shell command:

code"C:\Program Files\Erlang OTP\bin\erl.exe" -pa F:/1TB/P/workspace-IntelliJ-Erlang2/netconfClient/out/production/netconfClient -eval "netconfManager:open2()." -s init stop -noshell

Explanation:

  1. open2/0: This is the entry point for your application. It initializes the ETS table and starts the logic.
  2. init/0: Initializes the ct_attributes table and inserts sample data for testing purposes.
  3. start/0: Calls ets:select on the ct_attributes table with a proper match specification to retrieve matching records.

With these corrections, your application should now run without encountering the badarg error, and it will properly handle the ETS table selection.

Related blog posts