c++ - std::ifstream issue when running outside of IDE -


i have function works fine when running inside of visual studio debugging environment (with both debug , release configurations), when running app outside of ide, end-user do, program crashes. happens both debug , release builds.

i'm aware of differences can exist between debug , release configurations (optimizations, debug symbols, etc) , @ least aware of differences between running app inside visual studio versus outside of (debug heap, working directory, etc). i've looked @ several of these things , none seem address issue. first time posting so; can find solution existing posts i'm stumped!

i able attach debugger , oddly enough 2 different error messages, based on whether i'm running app on windows 7 versus windows 8.1. windows 7, error access violation , breaks right on return statement. windows 8.1, heap corruption error , breaks on construction of std::ifstream. in both cases, of local variables populated correctly know not matter of function not being able find file or read contents buffer data.

also interestingly, issue happens 20% of time on windows 8.1 , 100% of time on windows 7, though may have vastly different hardware these os's running on.

i'm not sure makes difference project type win32 desktop app , initializes directx 11. you'll notice file type interpreted binary, correct function loading compiled shaders.

here static member function loadfile:

hresult myclass::loadfile(_in_ const char* filename, _out_ byte** data, _out_ size_t* length) {     char pwd[max_path];     getcurrentdirectorya(max_path, pwd);     std::string fullfilepath = std::string(pwd) + "\\" + filename;      std::ifstream file(fullfilepath, std::ifstream::binary);      if (file)     {         file.seekg(0, file.end);         *length = (size_t)file.tellg();         file.seekg(0, file.beg);          *data = new byte[*length];          file.read(reinterpret_cast<char*>(*data), *length);          if (file) return s_ok;     }      return e_fail; } 

update:

interestingly, if allocate std::ifstream file on heap , not delete it, issue goes away. there must destruction of ifstream causing issue in case.

  • you don't check return value of getcurrentdirectorya - maybe current directory name long or something?

  • if using win32 (not portable!), use getfilesize file size rather doing seek

  • better yet, use boost write portable code

  • switch on warnings in compiler options

  • enable ios exceptions


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -