-2

I made a DirList Class to get all dir, file list in the specific dir. Below is the DirList Code.

class DirList {
private:
    std::string dir_path_;
    int type_;
    DIR *dir_;

public:
    DirList(std::string dir_path);
    DirList(std::string dir_path, int type);
    ~DirList();

    std::string GetNext();
};

DirList::DirList(std::string dir_path) {
    DirList(dir_path, 0);
}

DirList::DirList(std::string dir_path, int type) {
    dir_path_ = dir_path;
    type_ = type;
    dir_ = opendir(dir_path_.c_str());

    if (dir_ == NULL)
        std::cout << "DirList - opendir error - " << dir_path_;

    Logger(Logger::kError) << "path " << dir_path_ << " type " << type_;
    Logger(Logger::kError) << "address " << (uint64_t)this;
}

DirList::~DirList() {
    if (closedir(dir_) != 0)
        std::cout << "DirList - close error - " << dir_path_;
}

std::string DirList::GetNext() {
    Logger(Logger::kError) << "path " << dir_path_ << " type " << type_;
    Logger(Logger::kError) << "address " << (uint64_t)this;

    if (dir_ == NULL) {
        std::cout << "DirList - opendir error - " << dir_path_;
        return "";
    }

    dirent *dirent;
    if(type_ == 0){
        while ((dirent = readdir(dir_)) != NULL) {
            // Jump current and upper dir
            if (dirent->d_name[0] == '.')
                continue;
            else
                break;
        }
    }
    else{
        while ((dirent = readdir(dir_)) != NULL) {
            // Jump current and upper dir
            if ((dirent->d_name[0] == '.') || (dirent->d_type != type_))
                continue;
            else
                break;
        }
    }

    if (dirent == NULL)
        return "";

    return dirent->d_name;
}

DirList Class Use like below

// main() in main.cc
DirList dir_list(host_proc_path, DT_DIR); 
std::string pid;
while (!(pid = dir_list.GetNext()).empty()){
   ... // this working well.
}

// Parsing() Method in host_parser.cc - host_parser is class to get hostinfo.
DirList dir_list_nic(kSysNetPath);
std::string net_iface;
while (!(net_iface = dir_list_nic.GetNext()).empty()) {
   ... // this is'n't wokring. dir_path_ and type_ has wrong value.
}

DirList is working well in some code when this address in the constructor and this address in GetNext Method. But DirList isn't wokring in some code when this address is different... so dir_path_, type_ has wrong value.

I'cant understands why instance's address is changed. I do not address operation Only allocate memory by using new.

My Devel Env is Ubuntu 17.10 64bit and clang-++3.8. But I tried Ubuntu 16.04 64bit, clang-++4.0. clang-++5.0... But the same problem occurs.

thanks

Supsupi
  • 39
  • 5
  • 1
    Can you show how you use this class? It's worth noting you're not following the rule of three and you do have a raw pointer in your class. – Retired Ninja Apr 06 '18 at 02:09
  • I add usage code. – Supsupi Apr 06 '18 at 02:23
  • 1
    Unrelated: `DirList` violates the Rule Of Three. Sooner or later this will bite you. [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) Click the link and find out. – user4581301 Apr 06 '18 at 03:06

1 Answers1

0

I found the reason

DirList::DirList(std::string dir_path) { DirList(dir_path, 0); }

'DirList(dir_path, 0)' is not init this instance. It only makes temporary Instance.

To fix this DirList::DirList(std::string dir_path) : DirList(dir_path, 0) {}

Supsupi
  • 39
  • 5