title: Strings getting corrupted in ComboBox.AddObject. How to add them the proper way?
body: <p>I'm adding strings with objects (also strings) to a TComboBox, but getting corrupted strings when trying to retrieve them later.</p>
  
  <p>This is how I'm adding them:</p>
  
  <pre><code>var
    i: Integer;
    sl: TStringList;
    c: Integer;
    s: PChar;
  begin
  
    for i := 1 to tblCalls.FieldCount do
      if tblCalls.Fields[i - 1].Tag = 1 then
        ListBox1.Items.Append(tblCalls.Fields[i - 1].FieldName);
  
    sl := TStringList.Create;
    try
      LoadStyles(TStrings(sl));
  
      for c := 0 to sl.Count - 1 do
      begin
  
        s := PChar(sl.Values[sl.Names[c]]);
        ComboBox1.Items.AddObject(sl.Names[c], TObject(s));
      end;
  
    finally
      sl.Free;
    end;
  
  end;
  
  procedure LoadStyles(var AStylesList: TStrings);
  var
    f, n: String;
    filelist: TStringDynArray;
  begin
    f := ExtractFilePath(ParamStr(0)) + 'Styles';
    if (not DirectoryExists(f)) then
      Exit;
  
    filelist := TDirectory.GetFiles(f);
  
    for f in filelist do
    begin
      n := ChangeFileExt(ExtractFileName(f), EmptyStr);
      AStylesList.Add(n + '=' + f);
    end;
  end;
  </code></pre>
  
  <p>..and this is where I'm trying to retrieve a string object:</p>
  
  <pre><code>procedure TfrmOptions.ComboBox1Change(Sender: TObject);
  var
    si: TStyleInfo;
    i: Integer;
    s: String;
  begin
    i := TComboBox(Sender).ItemIndex;
    s := PChar(TComboBox(Sender).Items.Objects[i]);
  
    Showmessage(s); // --&gt; Mostly shows a corrupted string (gibberish chars)
  
    if (TStyleManager.IsValidStyle(s, si)) then
    begin
      if (not MatchStr(s, TStyleManager.StyleNames)) then
        TStyleManager.LoadFromFile(s);
      TStyleManager.TrySetStyle(si.Name);
    end;
  end;
  </code></pre>
  
  <p>I suspect that its something with the way I'm adding them. Perhaps I need to allocate memory at:</p>
  
  <pre><code>s := PChar(sl.Values[sl.Names[c]]);
  </code></pre>
  
  <p>Not sure. Looking at the help on StrNew, NewStr and StrAlloc, it says that those functions are deprecated. Can you help point out whats wrong?</p>