title: Same c++ code sometimes works and sometimes doesnt
body: <p>my c++ code works for a couple of times straight and after few executions it suddenly stops working and throws exceptions (without any changes!), and I cant figure out why.</p>
  
  <p>This is the problematic part of code:</p>
  
  <pre><code>STARTUPINFO si;
  PROCESS_INFORMATION pi;
  ZeroMemory(&amp;si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&amp;pi, sizeof(pi));
  TCHAR *path;
  SHGetKnownFolderPath(FOLDERID_Startup, KF_FLAG_CREATE, NULL, &amp;path);
  lstrcat(path, L"\\calc.exe");
  if (CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &amp;si, &amp;pi))
  {
      WaitForSingleObject(pi.hProcess, INFINITE);
      CloseHandle(pi.hProcess);
      CloseHandle(pi.hThread);
  
  }
  </code></pre>
  
  <p>After a few executions, 2 exceptions are thrown on the CreateProcess() line, the first one:</p>
  
  <pre><code>Unhandled exception at 0x779D8829 (ntdll.dll) in PS_Down.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77A15890).
  </code></pre>
  
  <p>The second:</p>
  
  <pre><code>Exception thrown at 0x77946111 (ntdll.dll) in PS_Down.exe: 0xC0000005: Access violation reading location 0x00000069. 
  </code></pre>
  
  <p>It happened to me with a few other projects (that don't include the CreateProcess() func) and iv'e noticed that it always happens when TCHAR and SHGetKnownFolderPath() are involved. 
  Any help with understanding how to fix the issue will be much appreciated, thanks in advance!</p>
  
  <p>P.S - I'm new to coding in cpp, so please try to explain accordingly</p>