Thread: losing pointers with destructor

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Also thinking the directory should always be const, is there a way to do that? (I guess keeping the vector private will have the same effect).
    Make all the member variables private, then only provide public member functions that do not change the state of the directory object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Frankly, other than the [...] ignore you in the future?
    I'm trying to help a newbie and you jump up to say "that was meaningless" without bothering to understand what I said? You get moody because I called you on your comment? You expect me to give more consideration to you than you give to me? You jump down on me and call me hostile?

    LMAO

    Who the hell do you think you are? Is this a joke? HAVEN'T WE HAD THIS CONVERSATION BEFORE?!

    You say you're going to ignore me? Considering that this is exemplar of our conversations would I notice the difference? Or would there just be less of you acting the part of put upon innocent because I don't offer you the preferential treatment you wrongly feel you deserve? I could get behind that. I'm certainly not going to give you a pass when screw up just because it might "really [make] [you] angry".

    Soma

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by phantomotap
    I'm trying to help a newbie and you jump up to say "that was meaningless" without bothering to understand what I said?
    When I made my comment, I was fairly sure I knew what your comment was about, because I had just answered the same question of MK27's myself.

    Quote Originally Posted by phantomotap
    You get moody because I called you on your comment?
    Looking at your reaction, you must have been pretty moody in order to respond the way you did.

    Quote Originally Posted by phantomotap
    You expect me to give more consideration to you than you give to me?
    No, I expect the same consideration.

    Quote Originally Posted by phantomotap
    You jump down on me and call me hostile?
    You are being hostile.

    Quote Originally Posted by phantomotap
    Who the hell do you think you are?
    An online acquaintance of yours from some time ago.

    Quote Originally Posted by phantomotap
    You say you're going to ignore me?
    No, I would like to know if you want me to ignore you because I keep misinterpreting you.

    Quote Originally Posted by phantomotap
    Or would there just be less of you acting the part of put upon innocent because I don't offer you the preferential treatment you wrongly feel you deserve?
    I do not see how just saying that I misinterpreted you is "preferential treatment".

    Quote Originally Posted by phantomotap
    I'm certainly not going to give you a pass when screw up just because it might "really [make] [you] angry".
    Neither am I going to give you a pass when you screw up, but I intend to phrase it such that I do not insult you while I am at it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by MK27 View Post
    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.

    One issue with storing the file info as you traverse the directory, tho, is that the memory required can turn out to be *huge*. An easy way around that is to simply pass callback functors to the traversing logic, and then just process each file "in place". Besides being more efficient, it'll probably run much faster, too.

    Here is something I posted a while back using such an approach.

    Anyway, it's good to see you're finally jumping into C++ coding. Good luck!

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    Well, you cannot really use inheritance based polymorphism anyway since the base class has no virtual member functions.
    Quote Originally Posted by phantomotap View Post
    I was going to give you the benefit of doubt. I've changed my mind. No explanation for you. That was stupid. You either jumped in with some random quip without bothering to understand what I was saying or you truly have no idea what you're talking about.
    Perhaps a third party view here would help.
    Phantomotap, in this instance I have to say that your comment above seemed out of line. I also misunderstood what you meant and could very well have posted a similar response to what laserlight did, making the same assumption. If you've been misunderstood, you only need to state that you have been misunderstood, and then explain your point in such a way as to clear up the misunderstanding.
    If my memory serves me well, you've provided valueable input in the past. So when you've put this behind you. I look forward to more valueable input from you. We all have our off days, just try and be a little more forgiving next time eh.

    On the other side of things, I too have inherrited from std::vector publicly a few times. Actually I've done it twice already in the project I'm working on! I'm using it purely as an alternative to declaring functions that take the vector as an argument, and am just declaring instances of this derived class within another class. No other members are added in the derived-from-vector class. For this I think it's quite appropriate.
    I believe I'm fully aware of what the effects of this are and I don't believe there are any noteworthy problems with this approach in this instance. In fact it has been used sucessfully in our production code in exactly this manner several times before.

    As always, there's an exception to every rule, and an exception quite likely doesn't apply to what the OP is doing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM