title: Unhandled Exception with c++ app on Visual Studio 2008 release build - occurs when returning from function
body: <p>I have a (rather large) application that I have written in C++ and until recently it has been running fine outside of visual studio from the release build. However, now, whenever I run it it says "Unhandled exception at 0x77cf205b in myprog.exe: 0xC0000005: Access violation writing location 0x45000200.", and leads me to "crtexe.c" at line 582 ("mainret = main(argc, argv, envp);") if I attempt to debug it. Note that this problem never shows if I run my debug executable outside of visual studio, or if I run my debug or release build within visual studio. It only happens when running the release build outside of visual studio.</p>
  
  <p>I have been through and put plenty of printfs and a couple of while(1)s in it to see when it actually crashed, and found that the access violation occurs at exactly the point that the value is returned from the function (I'm returning a pointer to an object). I don't fully understand why I would get an access violation at the point it returns, and it doesn't seem to matter what I'm returning as it still occurs when I return 0.</p>
  
  <p>The point it started crashing was when I added a function which does a lot of reading from a file using ifstream. I am opening the stream every time I attempt to read a new file and close it when I finish reading it.</p>
  
  <p>If I keep attempting to run it, it will run once in about 20 tries. It seems a lot more reliable if I run it off my pen drive (it seems to crash the first 3 or 4 times then run fine after that - maybe it's due to its slower read speed).</p>
  
  <p>Thanks for your help, and if I've missed anything let me know.</p>
  
  <p>EDIT: New info</p>
  
  <p>Well I removed the entirity of the function and replaced it with:</p>
  
  <pre><code>IndexedMesh * loadObj(char * objName)
  {
  
  ifstream fp_in;
  fp_in.open("lol.bmp", ios::in);
  
  fp_in.clear();
  fp_in.close();
  
  IndexedMesh * mesh = new IndexedMesh();
  
  printf("finished");
  
  
  return mesh;
  }
  </code></pre>
  
  <p>I also tried it with "return 0" and "return new IndexedMesh()". It's all fine until you put the ifstream stuff in. I do have 2 other ifstreams open in different functions (accessing completely different files). Could this be the problem?</p>
  
  <p>It actually errors on the return mesh line, (I got the debugger working with the separate release file). It completely nulls the mesh object when it attempts to return it.</p>