I wrote a class to stat directory contents, derived from vector. One of the methods is "subdirs" -- it returns a vector<directory>.
Code:
vector<directory> directory::subdirs() {
vector<directory> retv;
iterator it = begin();
while(it != end()) {
if (ftype(it) == 'd' && (*it).read) {
char dpath[PATH_MAX+1];
if ((*it).link) strcpy(dpath,(*it).link);
else sprintf(dpath, "%s/%s", path.c_str(), (*it).entry.d_name);
directory cur(dpath, stype, restat_links);
retv.push_back(cur);
}
it++;
}
return retv;
}
So push_back copies cur into the vector, then the destructor is called on cur because of scope. This causes a problem (can be a bus error or data corruption):
Code:
directory::~directory() {
/* vector<fileinfo>::iterator it = begin();
while (it != end()) {
if ((*it).link) free((*it).link);
it++;
}
if (stype) free(stype);*/
}
Commenting out that stuff solves the problem, I am assuming because of the pointers. This is my first week with C++, and there's a few hazy possible solutions in my mind,* but I wanted to ask and see if there is a strategy here more obvious to the experienced.
Thanks in advance!
* eg, stype and link are pointers to malloc'd C-strings, I could make them normal (copyable) arrays.