file: Fix memory errors

Apparently it's not okay to return str.c_str() to caller functions,
because str will go out of scope...

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-05-18 10:15:51 -04:00
parent 6dc8bf7329
commit 44f93d85e4
2 changed files with 12 additions and 12 deletions

View File

@ -21,14 +21,14 @@ private:
std::string filename;
unsigned int version;
const char *find_dir();
const std::string find_dir();
bool open_read();
bool open_write();
public:
File(const std::string &);
~File();
const char *get_filepath();
const std::string get_filepath();
const unsigned int get_version();
bool exists();
bool open(OpenMode);

View File

@ -25,22 +25,22 @@ File :: ~File()
close();
}
const char *File :: find_dir()
const std::string File :: find_dir()
{
std::string res = g_get_user_data_dir();
std::string res(g_get_user_data_dir());
res += "/" + OCARINA_DIR;
return res.c_str();
return res;
}
const char *File :: get_filepath()
const std::string File :: get_filepath()
{
std::string res;
std::string res = "";
if (filename != "") {
res = find_dir();
res += "/" + filename;
}
return res.c_str();
return res;
}
const unsigned int File :: get_version()
@ -50,7 +50,7 @@ const unsigned int File :: get_version()
bool File :: exists()
{
return g_file_test(get_filepath(), G_FILE_TEST_EXISTS);
return g_file_test(get_filepath().c_str(), G_FILE_TEST_EXISTS);
}
bool File :: open_read()
@ -58,7 +58,7 @@ bool File :: open_read()
if (!exists())
return false;
std::fstream::open(get_filepath(), std::fstream::in);
std::fstream::open(get_filepath().c_str(), std::fstream::in);
if (std::fstream::fail())
return false;
@ -70,10 +70,10 @@ bool File :: open_read()
bool File :: open_write()
{
if (g_mkdir_with_parents(find_dir(), 0755) != 0)
if (g_mkdir_with_parents(find_dir().c_str(), 0755) != 0)
return false;
std::fstream::open(get_filepath(), std::fstream::out);
std::fstream::open(get_filepath().c_str(), std::fstream::out);
if (std::fstream::fail())
return false;