bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

cursesw.h 5.6: copy operators incorrect


From: Stephan Beal
Subject: cursesw.h 5.6: copy operators incorrect
Date: Thu, 27 Sep 2007 18:32:24 +0200

Hello, ncurses maintainers!

i'm just browsing through the ncurses++ source code (nothing better to do, i guess ;) and came across these:

  NCursesWindow& operator=(const NCursesWindow& rhs)
  {
    if (this != &rhs)
      *this = rhs;
    return *this;
  }

  NCursesWindow(const NCursesWindow& rhs)
    : w(rhs.w), alloced(rhs.alloced), par(rhs.par), subwins(rhs.subwins), sib(rhs.sib )
  {
  }


My first comment is that the operator=() is implemented in terms of itself and is in effect a no-op. The (this!=&rhs) will cause (*this=rhs) to be called, which is the same as (this->operator=( *this )), which will in turn simply do (return *this).

My second comment is that the copy ctor seems to cause unusual/unexpected behaviour: as soon as one of the windows (either lhs or rhs) is destroyed, the subwindows will be wiped out and for the other object this->w will end up in a useless state and be lined up for a double-delwin():

From ~NCursesWindow:

    if (alloced && w != 0)
        ::delwin(w);

Suppose that rhs.alloced is true and the rhs object goes out of scope. That will wipe out (rhs->w), but leave it laying around (but broken) in lhs->w. It seems to me that it will only work as-designed (because of the alloced flag) if both lhs and rhs are deleted via the destruction of their parent window (not if a user simply calls (delete lhs)).

And it seems that kil_subwindows only does have of its job - calling delwin(). It doesn't actually delete the child NCursesWindows, which (if i understand the code correctly) leads to a memory leak when (delete parentWindow) is used.

IMO the only correct implementation for the copy operator/ctor is no implementation - declare them as private and don't define them, so the compile will fail if someone tries to do it. Copying of Window objects can lead to too many problems, and shouldn't, IMO, be allowed.

Happy hacking!

--
----- stephan beal
http://wanderinghorse.net/home/stephan/
http://s11n.net/home/stephan/
reply via email to

[Prev in Thread] Current Thread [Next in Thread]