From 4ab31c98e6b0520c0c4bf8dd1f5f56ee588c9de0 Mon Sep 17 00:00:00 2001 From: Caroline Tice Date: Tue, 12 Oct 2010 21:57:09 +0000 Subject: [PATCH] Fix some memory leaks. Add call to lldb.SBDebugger.Initialize() to lldb.py, so it automatically gets called when the lldb Python module gets loaded. llvm-svn: 116345 --- lldb/scripts/Python/append-debugger-id.py | 1 + lldb/source/Breakpoint/BreakpointIDList.cpp | 1 - .../source/Interpreter/CommandInterpreter.cpp | 21 ++++++------- lldb/tools/driver/Driver.cpp | 31 ++++++++++++------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/lldb/scripts/Python/append-debugger-id.py b/lldb/scripts/Python/append-debugger-id.py index ab119a86c402..26460c8dc401 100644 --- a/lldb/scripts/Python/append-debugger-id.py +++ b/lldb/scripts/Python/append-debugger-id.py @@ -21,6 +21,7 @@ except IOError: print "Error: Unable to open file for appending: " + output_name else: f_out.write ("debugger_unique_id = 0\n"); + f_out.write ("lldb.SBDebugger.Initialize()\n"); try: f_out.close() except IOError: diff --git a/lldb/source/Breakpoint/BreakpointIDList.cpp b/lldb/source/Breakpoint/BreakpointIDList.cpp index 3f3e8a15f915..50a89c720669 100644 --- a/lldb/source/Breakpoint/BreakpointIDList.cpp +++ b/lldb/source/Breakpoint/BreakpointIDList.cpp @@ -181,7 +181,6 @@ BreakpointIDList::FindAndReplaceIDRanges (Args &old_args, Target *target, Comman if (BreakpointIDList::StringContainsIDRangeExpression (current_arg, &range_start_len, &range_end_pos)) { is_range = true; - range_start = (char *) malloc (range_start_len + 1); range_start.assign (current_arg, range_start_len); range_end = current_arg + range_end_pos; } diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 64bdfcd5b80e..4a87ba0359d6 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include +#include #include #include @@ -947,13 +948,10 @@ CommandInterpreter::HasAliasOptions () } void -CommandInterpreter::BuildAliasCommandArgs -( - CommandObject *alias_cmd_obj, - const char *alias_name, - Args &cmd_args, - CommandReturnObject &result -) +CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj, + const char *alias_name, + Args &cmd_args, + CommandReturnObject &result) { OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); @@ -970,10 +968,9 @@ CommandInterpreter::BuildAliasCommandArgs OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); int old_size = cmd_args.GetArgumentCount(); - int *used = (int *) malloc ((old_size + 1) * sizeof (int)); - - memset (used, 0, (old_size + 1) * sizeof (int)); - used[0] = 1; + std::vector used (old_size + 1, false); + + used[0] = true; for (int i = 0; i < option_arg_vector->size(); ++i) { @@ -1002,7 +999,7 @@ CommandInterpreter::BuildAliasCommandArgs else { new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); - used[index] = 1; + used[index] = true; } } } diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp index 13f7fc6fe3d6..9887e35850a9 100644 --- a/lldb/tools/driver/Driver.cpp +++ b/lldb/tools/driver/Driver.cpp @@ -332,7 +332,8 @@ ShowUsage (FILE *out, lldb::OptionDefinition *option_table, Driver::OptionData d } void -BuildGetOptTable (lldb::OptionDefinition *expanded_option_table, struct option **getopt_table, uint32_t num_options) +BuildGetOptTable (lldb::OptionDefinition *expanded_option_table, std::vector &getopt_table, + uint32_t num_options) { if (num_options == 0) return; @@ -341,25 +342,27 @@ BuildGetOptTable (lldb::OptionDefinition *expanded_option_table, struct option * uint32_t j; std::bitset<256> option_seen; + getopt_table.resize (num_options + 1); + for (i = 0, j = 0; i < num_options; ++i) { char short_opt = expanded_option_table[i].short_option; if (option_seen.test(short_opt) == false) { - (*getopt_table)[j].name = expanded_option_table[i].long_option; - (*getopt_table)[j].has_arg = expanded_option_table[i].option_has_arg; - (*getopt_table)[j].flag = NULL; - (*getopt_table)[j].val = expanded_option_table[i].short_option; + getopt_table[j].name = expanded_option_table[i].long_option; + getopt_table[j].has_arg = expanded_option_table[i].option_has_arg; + getopt_table[j].flag = NULL; + getopt_table[j].val = expanded_option_table[i].short_option; option_seen.set(short_opt); ++j; } } - (*getopt_table)[j].name = NULL; - (*getopt_table)[j].has_arg = 0; - (*getopt_table)[j].flag = NULL; - (*getopt_table)[j].val = 0; + getopt_table[j].name = NULL; + getopt_table[j].has_arg = 0; + getopt_table[j].flag = NULL; + getopt_table[j].val = 0; } @@ -456,6 +459,7 @@ Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit) SBError error; std::string option_string; struct option *long_options = NULL; + std::vector long_options_vector; uint32_t num_options; for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options) @@ -468,9 +472,12 @@ Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit) return error; } - long_options = (struct option *) malloc ((num_options + 1) * sizeof (struct option)); + BuildGetOptTable (g_options, long_options_vector, num_options); - BuildGetOptTable (g_options, &long_options, num_options); + if (long_options_vector.empty()) + long_options = NULL; + else + long_options = &long_options_vector.front(); if (long_options == NULL) { @@ -625,7 +632,9 @@ Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit) error.SetErrorStringWithFormat ("invalid option with value %i", val); } if (error.Fail()) + { return error; + } } }