Change DebugCanvas API to not encourage memory leaks

Pass command strings and offset arrays as out parameters instead of
returning new arrays from the functions.

This simplifies debugger leak investigations, as the app leaks less by
design.

Review URL: https://codereview.chromium.org/821663003
diff --git a/debugger/QT/SkDebuggerGUI.cpp b/debugger/QT/SkDebuggerGUI.cpp
index 949137f..25f7036 100644
--- a/debugger/QT/SkDebuggerGUI.cpp
+++ b/debugger/QT/SkDebuggerGUI.cpp
@@ -773,11 +773,6 @@
     SkSafeUnref(stream);
     SkSafeUnref(picture);
 
-    // Will this automatically clear out due to nature of refcnt?
-    SkTArray<SkString>* commands = fDebugger.getDrawCommandsAsStrings();
-    SkTDArray<size_t>* offsets = fDebugger.getDrawCommandOffsets();
-    SkASSERT(commands->count() == offsets->count());
-
     fActionProfile.setDisabled(false);
 
     /* fDebugCanvas is reinitialized every load picture. Need it to retain value
@@ -787,8 +782,8 @@
      * */
     fDebugger.highlightCurrentCommand(fSettingsWidget.getVisibilityFilter());
 
-    this->setupListWidget(commands, offsets);
-    this->setupComboBox(commands);
+    this->setupListWidget();
+    this->setupComboBox();
     this->setupOverviewText(NULL, 0.0, 1);
     fInspectorWidget.setDisabled(false);
     fSettingsWidget.setDisabled(false);
@@ -801,31 +796,32 @@
     actionPlay();
 }
 
-void SkDebuggerGUI::setupListWidget(SkTArray<SkString>* commands, SkTDArray<size_t>* offsets) {
-    SkASSERT(commands->count() == offsets->count());
+void SkDebuggerGUI::setupListWidget() {
     fListWidget.clear();
     int counter = 0;
     int indent = 0;
-    for (int i = 0; i < commands->count(); i++) {
+    for (int i = 0; i < fDebugger.getSize(); i++) {
         QListWidgetItem *item = new QListWidgetItem();
-        item->setData(Qt::DisplayRole, (*commands)[i].c_str());
+        SkDrawCommand* command = fDebugger.getDrawCommandAt(i);
+        SkString commandString = command->toString();
+        item->setData(Qt::DisplayRole, commandString.c_str());
         item->setData(Qt::UserRole + 1, counter++);
 
-        if (0 == strcmp("Restore", (*commands)[i].c_str()) ||
-            0 == strcmp("EndCommentGroup", (*commands)[i].c_str())) {
+        if (0 == strcmp("Restore", commandString.c_str()) ||
+            0 == strcmp("EndCommentGroup", commandString.c_str())) {
             indent -= 10;
         }
 
         item->setData(Qt::UserRole + 3, indent);
 
-        if (0 == strcmp("Save", (*commands)[i].c_str()) ||
-            0 == strcmp("Save Layer", (*commands)[i].c_str()) ||
-            0 == strcmp("BeginCommentGroup", (*commands)[i].c_str())) {
+        if (0 == strcmp("Save", commandString.c_str()) ||
+            0 == strcmp("Save Layer", commandString.c_str()) ||
+            0 == strcmp("BeginCommentGroup", commandString.c_str())) {
             indent += 10;
         }
 
         item->setData(Qt::UserRole + 4, -1);
-        item->setData(Qt::UserRole + 5, (int)(*offsets)[i]);
+        item->setData(Qt::UserRole + 5, (int) command->offset());
 
         fListWidget.addItem(item);
     }
@@ -845,13 +841,13 @@
     fInspectorWidget.setText(clipStack.c_str(), SkInspectorWidget::kClipStack_TabType);
 }
 
-void SkDebuggerGUI::setupComboBox(SkTArray<SkString>* command) {
+void SkDebuggerGUI::setupComboBox() {
     fFilter.clear();
     fFilter.addItem("--Filter By Available Commands--");
 
     std::map<std::string, int> map;
-    for (int i = 0; i < command->count(); i++) {
-        map[(*command)[i].c_str()]++;
+    for (int i = 0; i < fDebugger.getSize(); i++) {
+        map[fDebugger.getDrawCommandAt(i)->toString().c_str()]++;
     }
 
     for (std::map<std::string, int>::iterator it = map.begin(); it != map.end();
diff --git a/debugger/QT/SkDebuggerGUI.h b/debugger/QT/SkDebuggerGUI.h
index 3f44550..2a514ba 100644
--- a/debugger/QT/SkDebuggerGUI.h
+++ b/debugger/QT/SkDebuggerGUI.h
@@ -334,14 +334,14 @@
     void saveToFile(const SkString& filename);
 
     /**
-        Populates the list widget with the vector of strings passed in.
+        Populates the list widget with the debugger draw command info.
      */
-    void setupListWidget(SkTArray<SkString>* commands, SkTDArray<size_t>* offsets);
+    void setupListWidget();
 
     /**
-        Populates the combo box widget with the vector of strings passed in.
+        Populates the combo box widget with with the debugger draw command info.
      */
-    void setupComboBox(SkTArray<SkString>* command);
+    void setupComboBox();
 
     /**
         Fills in the overview pane with text
diff --git a/debugger/SkDebugger.h b/debugger/SkDebugger.h
index 5359b10..683b98d 100644
--- a/debugger/SkDebugger.h
+++ b/debugger/SkDebugger.h
@@ -44,12 +44,8 @@
         fDebugCanvas->toggleCommand(index, isVisible);
     }
 
-    SkTArray<SkString>* getDrawCommandsAsStrings() {
-        return fDebugCanvas->getDrawCommandsAsStrings();
-    }
-
-    SkTDArray<size_t>* getDrawCommandOffsets() {
-        return fDebugCanvas->getDrawCommandOffsets();
+    SkDrawCommand* getDrawCommandAt(int index) {
+        return fDebugCanvas->getDrawCommandAt(index);
     }
 
     const SkTDArray<SkDrawCommand*>& getDrawCommands() const {
@@ -66,7 +62,7 @@
 
     SkPicture* copyPicture();
 
-    int getSize() {
+    int getSize() const {
         return fDebugCanvas->getSize();
     }
 
diff --git a/platform_tools/nacl/src/nacl_debugger.cpp b/platform_tools/nacl/src/nacl_debugger.cpp
index b80dde0..e7e26b8 100644
--- a/platform_tools/nacl/src/nacl_debugger.cpp
+++ b/platform_tools/nacl/src/nacl_debugger.cpp
@@ -76,11 +76,10 @@
                 picture->unref();
 
                 // Set up the command list.
-                SkTArray<SkString>* commands = fDebugger.getDrawCommandsAsStrings();
                 PostMessage("ClearCommands");
-                for (int i = 0; i < commands->count(); ++i) {
+                for (int i = 0; i < fDebugger.getSize(); ++i) {
                     SkString addCommand("AddCommand:");
-                    addCommand.append((*commands)[i]);
+                    addCommand.append(fDebugger.getDrawCommandAt(i)->toString());
                     PostMessage(addCommand.c_str());
                 }
                 PostMessage("UpdateCommands");
diff --git a/src/utils/debugger/SkDebugCanvas.cpp b/src/utils/debugger/SkDebugCanvas.cpp
index f2f92b8..46c0b8b 100644
--- a/src/utils/debugger/SkDebugCanvas.cpp
+++ b/src/utils/debugger/SkDebugCanvas.cpp
@@ -388,27 +388,6 @@
     return fCommandVector;
 }
 
-// TODO(chudy): Free command string memory.
-SkTArray<SkString>* SkDebugCanvas::getDrawCommandsAsStrings() const {
-    SkTArray<SkString>* commandString = new SkTArray<SkString>(fCommandVector.count());
-    if (!fCommandVector.isEmpty()) {
-        for (int i = 0; i < fCommandVector.count(); i ++) {
-            commandString->push_back() = fCommandVector[i]->toString();
-        }
-    }
-    return commandString;
-}
-
-SkTDArray<size_t>* SkDebugCanvas::getDrawCommandOffsets() const {
-    SkTDArray<size_t>* commandOffsets = new SkTDArray<size_t>;
-    if (!fCommandVector.isEmpty()) {
-        for (int i = 0; i < fCommandVector.count(); i ++) {
-            *commandOffsets->push() = fCommandVector[i]->offset();
-        }
-    }
-    return commandOffsets;
-}
-
 void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level) {
     if (NULL == fTexOverrideFilter) {
         fTexOverrideFilter = new SkTexOverrideFilter;
diff --git a/src/utils/debugger/SkDebugCanvas.h b/src/utils/debugger/SkDebugCanvas.h
index 18f4c8d..a526525 100644
--- a/src/utils/debugger/SkDebugCanvas.h
+++ b/src/utils/debugger/SkDebugCanvas.h
@@ -123,16 +123,6 @@
     SkTDArray<SkDrawCommand*>& getDrawCommands();
 
     /**
-     * Returns the string vector of draw commands
-     */
-    SkTArray<SkString>* getDrawCommandsAsStrings() const;
-
-    /**
-     * Returns an array containing an offset (in the SkPicture) for each command
-     */
-    SkTDArray<size_t>* getDrawCommandOffsets() const;
-
-    /**
         Returns length of draw command vector.
      */
     int getSize() const {