title: some odd corruption issues
body: <p>Here's some snippets of a winamp plugin I've been modifying for outputting visual data over a serial port</p>
  
  <pre><code>char * overrideCom = NULL;
  char * cPortName;
  
  void config(struct winampVisModule *this_mod)
  {
      MessageBox(this_mod-&gt;hwndParent, cPortName, "Serial Port", MB_OK); // Tell us what the value is
  }
  
  // This function will convert a System^ string into a std::string
  std::string makeStd(String ^in){
      array&lt;Byte, 1&gt; ^chars = System::Text::Encoding::ASCII-&gt;GetBytes(in);
      pin_ptr&lt;Byte&gt; charsPointer = &amp;(chars[0]);
      char *nativeCharsPointer = reinterpret_cast&lt;char *&gt;(static_cast&lt;unsigned char *&gt;(charsPointer));
      std::string native(nativeCharsPointer, chars-&gt;Length);
      return nativeCharsPointer;
  }
  
  // This function will grab the second indexed com port and return it in an easily convertable std::string instead of String^
  std::string getComPort(){
      array&lt;String^, 1&gt; ^serialPorts = nullptr;
      LPTSTR out;
      try {
          serialPorts = SerialPort::GetPortNames();
      }
      catch (Win32Exception^ ex)
      {
          MessageBox(NULL, "Failed to find COM port!","Initialize Failed!", MB_OK);
      }
  
      std::string portb = makeStd(serialPorts[1]);
      out = const_cast&lt;char *&gt;(portb.c_str());
      // Make sure I'm not crazy:
      MessageBox(NULL, out, "Com port!", MB_OK);
      return portb;
  }
  
  int init(struct winampVisModule *this_mod)
  {
      config_read(this_mod);
  
      //std::string port = getComPort();
      //cPortName = const_cast&lt;char *&gt;(port.c_str());
  
      // This would normally work but for some reason causes corruption of the string and doesn't connect properly:
  
      if (overrideCom == NULL || overrideCom[0] == 0){
          std::string portx = getComPort();
          cPortName = const_cast&lt;char *&gt;(portx.c_str());
      }
      else
      {
          cPortName = overrideCom;
      }
  
  return 0;
  }
  
  void config_getinifn(struct winampVisModule *this_mod, char *ini_file)
  {   // makes a .ini file in the winamp directory named "plugin.ini"
      char *p;
      GetModuleFileName(this_mod-&gt;hDllInstance,ini_file,MAX_PATH);
      p=ini_file+strlen(ini_file);
      while (p &gt;= ini_file &amp;&amp; *p != '\\') p--;
      if (++p &gt;= ini_file) *p = 0;
      strcat(ini_file,"plugin.ini");
  }
  
  
  void config_read(struct winampVisModule *this_mod)
  {
      char ini_file[MAX_PATH];
      char* tResult = new char[255];
      config_getinifn(this_mod,ini_file);
      config_x = GetPrivateProfileInt(this_mod-&gt;description,"Screen_x",config_x,ini_file);
      config_y = GetPrivateProfileInt(this_mod-&gt;description,"Screen_y",config_y,ini_file);
      // Grab the overrideCom= param.
      GetPrivateProfileString(this_mod-&gt;description, "overrideCom", NULL, tResult, 255, ini_file);
      overrideCom = tResult;
  }
  </code></pre>
  
  <p>When I run the code without an overrideCom parameter, it is supposed to compensate by grabbing the 2nd indexed com port it can find, alone it works fine:</p>
  
  <pre><code>std::string port = getComPort();
  cPortName = const_cast&lt;char *&gt;(port.c_str());
  </code></pre>
  
  <p>but this does not work:</p>
  
  <pre><code>if (overrideCom == NULL || overrideCom[0] == 0){
      std::string portx = getComPort();
      cPortName = const_cast&lt;char *&gt;(portx.c_str());
  }
  else
  {
      cPortName = overrideCom;
  }
  </code></pre>
  
  <p>The port does not connect and when I call config() I get a message box saying:</p>
  
  <pre><code>Èêtw
  </code></pre>
  
  <p>When I omit the above if statement, config() outputs:</p>
  
  <pre><code>COM3
  </code></pre>
  
  <p>I don't get it!</p>
  
  <p>ALSO,
  when an overrideCom parameter is specified in the INI file, it works perfectly.</p>
  
  <p>My goal is to not rely on the INI file and allow for it to autodetect the 2nd com port, but allow for an override in the INI if needed.</p>
  
  <hr>
  
  <p><strong>DOH! The solution was to declare portx as a global variable.</strong></p>