|
From: <mor...@us...> - 2008-11-17 10:29:06
|
Revision: 3288
http://ecell.svn.sourceforge.net/ecell/?rev=3288&view=rev
Author: moriyoshi
Date: 2008-11-17 10:29:01 +0000 (Mon, 17 Nov 2008)
Log Message:
-----------
- Plug leaks in VVector.
Modified Paths:
--------------
ecell3/branches/ecell-3.1/ecell/libecs/VVector.cpp
ecell3/branches/ecell-3.1/ecell/libecs/VVector.h
Modified: ecell3/branches/ecell-3.1/ecell/libecs/VVector.cpp
===================================================================
--- ecell3/branches/ecell-3.1/ecell/libecs/VVector.cpp 2008-11-10 20:13:01 UTC (rev 3287)
+++ ecell3/branches/ecell-3.1/ecell/libecs/VVector.cpp 2008-11-17 10:29:01 UTC (rev 3288)
@@ -140,10 +140,6 @@
#include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */
-#ifdef _MSC_VER
-#define strdup( x ) _strdup( x )
-#endif /* _MSC_VER */
-
#if defined( WIN32 ) && !defined( __CYGWIN__ )
#define PATH_SEPARATOR '\\'
#else
@@ -163,9 +159,9 @@
namespace libecs {
int vvectorbase::_serialNumber = 0;
-char const *vvectorbase::_defaultDirectory = NULL;
+std::string vvectorbase::_defaultDirectory;
int vvectorbase::_directoryPriority = 999;
-std::vector<char const *> vvectorbase::_tmp_name;
+std::vector<std::string> vvectorbase::_tmp_name;
std::vector<int> vvectorbase::_file_desc_read;
std::vector<int> vvectorbase::_file_desc_write;
bool vvectorbase::_atexitSet = false;
@@ -240,15 +236,15 @@
vvectorbase::vvectorbase()
{
- if (_defaultDirectory == NULL) {
+ if (_defaultDirectory.empty()) {
char const *envVal = getenv("VVECTORTMPDIR");
if (envVal != NULL) {
- _defaultDirectory = strdup(envVal);
+ _defaultDirectory = envVal;
_directoryPriority = 3;
}
else
{
- _defaultDirectory = strdup( get_temp_dir() );
+ _defaultDirectory = get_temp_dir();
_directoryPriority = 4;
}
}
@@ -279,11 +275,7 @@
void vvectorbase::unlinkfile()
{
- if (_file_name) {
- unlink(_file_name); // ignore error
- free(_file_name);
- _file_name = NULL;
- }
+ unlink(_file_name.c_str()); // ignore error
}
@@ -293,18 +285,13 @@
assert(dirname[0] != '\0');
if (priority < _directoryPriority) {
_directoryPriority = priority;
- if (_defaultDirectory != NULL) {
- free(const_cast<char*>(_defaultDirectory));
- }
- _defaultDirectory = strdup(dirname);
+ _defaultDirectory = dirname;
}
}
void vvectorbase::removeTmpFile()
{
- std::vector<char const *>::iterator iii;
-
#ifndef OPEN_WHEN_ACCESS
std::vector<int>::iterator ii;
for (ii = _file_desc_read.begin(); ii != _file_desc_read.end(); ii++) {
@@ -323,38 +310,39 @@
#endif /* OPEN_WHEN_ACCESS */
- for (iii = _tmp_name.begin(); iii != _tmp_name.end(); iii++) {
-
- unlink (*iii);
+ for (std::vector<std::string>::iterator iii= _tmp_name.begin();
+ iii != _tmp_name.end(); iii++) {
+ unlink ((*iii).c_str());
}
}
void vvectorbase::initBase(char const * const dirname)
{
- char pathname[256];
- char filename[256];
+ std::string pathname;
if (dirname != NULL) {
- strcpy(pathname, dirname);
+ pathname = dirname;
} else {
- strcpy(pathname, _defaultDirectory);
+ pathname = _defaultDirectory;
}
- if (pathname[strlen(pathname) - 1] != PATH_SEPARATOR) {
- static const char sep[2] = { PATH_SEPARATOR, 0 };
- strcat(pathname, sep);
+ if ( pathname.size() == 0 || pathname[ pathname.size() - 1 ] != PATH_SEPARATOR) {
+ pathname += PATH_SEPARATOR;
}
- if (osif_is_dir(pathname) == 0) {
+ if (osif_is_dir( pathname.c_str() ) == 0) {
throw vvector_init_error();
}
- checkDiskFull(pathname, 1);
- sprintf(filename, "vvector-%ld-%04d",
- osif_get_pid(), _myNumber);
- strcat(pathname, filename);
- _file_name = strdup(pathname);
- _tmp_name.push_back(_file_name);
+ checkDiskFull(pathname.c_str(), 1);
+ {
+ char filename[256];
+ snprintf(filename, sizeof(filename) - 1, "vvector-%ld-%04d",
+ osif_get_pid(), _myNumber);
+ pathname += filename;
+ }
+ _file_name = pathname;
+ _tmp_name.push_back(pathname);
- _fdw = open(_file_name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY |O_LARGEFILE, 0600);
- _fdr = open(_file_name, O_RDONLY | O_BINARY | O_LARGEFILE );
+ _fdw = open(_file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY |O_LARGEFILE, 0600);
+ _fdr = open(_file_name.c_str(), O_RDONLY | O_BINARY | O_LARGEFILE );
if (_fdw < 0)
{
@@ -366,27 +354,23 @@
throw vvector_init_error();
}
- #ifndef OPEN_WHEN_ACCESS
+#ifndef OPEN_WHEN_ACCESS
_file_desc_write.push_back(_fdr);
_file_desc_write.push_back(_fdw);
- #endif /*OPEN_WHEN_ACCESS*/
-
- #ifdef OPEN_WHEN_ACCES
+#else
close(_fdw);
_fdw = -1;
close(_fdr);
_fdr = -1;
- #endif /*OPEN_WHEN_ACCESS*/
+#endif /*OPEN_WHEN_ACCESS*/
-
-
}
void vvectorbase::my_open_to_append()
{
- checkDiskFull(_file_name, 0);
- _fdw = open(_file_name, O_WRONLY | O_BINARY |O_LARGEFILE);
+ checkDiskFull(_file_name.c_str(), 0);
+ _fdw = open(_file_name.c_str(), O_WRONLY | O_BINARY |O_LARGEFILE);
if (_fdw < 0)
{
throw vvector_write_error();
@@ -402,7 +386,7 @@
void vvectorbase::my_open_to_read(off_t offset)
{
if (_fdr<0) {
- _fdr = open(_file_name, O_RDONLY | O_BINARY|O_LARGEFILE );
+ _fdr = open(_file_name.c_str(), O_RDONLY | O_BINARY|O_LARGEFILE );
}
if (_fdr < 0) {
throw vvector_read_error();
Modified: ecell3/branches/ecell-3.1/ecell/libecs/VVector.h
===================================================================
--- ecell3/branches/ecell-3.1/ecell/libecs/VVector.h 2008-11-10 20:13:01 UTC (rev 3287)
+++ ecell3/branches/ecell-3.1/ecell/libecs/VVector.h 2008-11-17 10:29:01 UTC (rev 3288)
@@ -143,6 +143,7 @@
#define __VVECTOR_H__
#include <vector>
+#include <string>
#if !defined(HAVE_SSIZE_T)
typedef int ssize_t;
@@ -173,41 +174,40 @@
const unsigned int VVECTOR_WRITE_CACHE_INDEX_SIZE = 2;
class vvector_full : public std::exception {
- public:
+public:
+ virtual char const* what() throw()
+ {
+ return "Total disk space or allocated space is full.\n";
+ }
+};
- virtual char const* what() throw()
- {
- return "Total disk space or allocated space is full.\n";
- }
- };
-
class vvector_write_error : public std::exception {
- public:
+public:
- virtual char const* what() throw()
- {
- return "I/O error while attempting to write on disk.\n";
- }
- };
+ virtual char const* what() throw()
+ {
+ return "I/O error while attempting to write on disk.\n";
+ }
+};
class vvector_read_error : public std::exception {
- public:
+public:
- virtual char const* what() throw()
- {
- return "I/O error while attempting to read from disk.\n";
- }
- };
+ virtual char const* what() throw()
+ {
+ return "I/O error while attempting to read from disk.\n";
+ }
+};
class vvector_init_error : public std::exception {
- public:
+public:
- virtual char const* what() throw()
- {
- return "VVector initialization error.\n";
- }
- };
+ virtual char const* what() throw()
+ {
+ return "VVector initialization error.\n";
+ }
+};
class vvectorbase {
// types
@@ -216,10 +216,10 @@
// private valiables
private:
static int _serialNumber;
- static char const *_defaultDirectory;
+ static std::string _defaultDirectory;
static int _directoryPriority;
- static std::vector<char const *> _tmp_name;
+ static std::vector<std::string> _tmp_name;
static std::vector<int> _file_desc_read;
static std::vector<int> _file_desc_write;
@@ -231,7 +231,7 @@
// protected variables
protected:
int _myNumber;
- char *_file_name;
+ std::string _file_name;
int _fdr,_fdw;
void unlinkfile();
@@ -242,6 +242,9 @@
void my_close_read();
void my_close_write();
+ private:
+ vvectorbase( vvectorbase const& );
+
// constructor, destructor
public:
vvectorbase();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|