This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: BUG - Cygwin to GNU CC compatibility


Arash,

If your program runs on a bunch of systems, well, you're just lucky,
because the bugs are in your code, not in cygwin.  You've provided a
constructor (whitespace edited to make this email shorter)

DigitList::DigitList(DigitList const &diglst) {
    *this = diglst;
};

which just invokes, implicitly, the default copy constructor. This
default copy constructor would look something like this:

DigitList::DigitList(DigitList &diglst) {
    this.digitList = diglist.digitList; // *** This line
    this.listSize = diglist.listSize;
}

The line I've marked with a *** is the trouble maker -- it makes an
alias for the pointer member digitList (which is effectively an
int*). When you copy a DigitList using this copy ctor, you get two
DigitLists sharing a single digitList pointer.

Now, in your dtor:

DigitList::~DigitList() {
   listSize = 0;
   free(digitList);   
};

you free digitList. But if two objects sharing a digitList both delete
the same pointer, then you've crossed over into the realm of undefined
behaviour. The implementation is allowed to do anything now: it can
keep running without a problem, it can crash (as cygwin's newlib is
apparently doing) or it can send dirty pictures of itself to the
White House.

Anyway, so you should THANK cygwin for finding the bugs in your
code. BTW, 1) why are you using free and malloc instead of new and
delete, anyway? and 2) Why are you writing a class like DigitList in the
first place -- why not simply use a vector<int>? and 3) I've only
pointed out ONE of your memory management bugs. There are plenty more
in this code.


I think Arash Partow wrote:
[Charset iso-8859-1 unsupported, filtering to ASCII...]
> Hi all,
> 
> I've found a certain combination of simple code that is totally compatible
> with unix/linux GNU compilers and runs perfectly on those OSs however
> even though it compiles fine using Cygwin g++, causes the program to crash
> under windows when executed.
> 
> The demo program is a simple counter example written in C++, no fancy
> code, or special coding tricks have been used. The only libs used are:
> stdio, stdlib and iostream.
> 
> The error occurs on a particular malloc, as state before the program runs
> fine on all unix based systems and I was able to compile it with Borland C++
> Builder 6.0 and the produced executable ran fine on windows.
> 
> I've tried running the cygwin produced executable on Win95,98,2K,XP,NT
> and on all these OSs the program will crash.
> 
> I'm using the latest version of cygwin compiler system as of the 2nd of Aug.
> I have no idea on how to go around fixing a patch for this cause i'm not
> sure
> where to begin, there are a number of places for which such an error like
> this
> can occur, I think it maybe something to do with the Cygwin DLL.
> 
> I have uploaded the sourcode, makefile, executable  and a copy of my cygwin
> checkout, the url is:
> http://www.froggy.com.au/arash/cygwin/cygwin_bug.zip
> 
> to make the application type:
> make all
> 
> to run the application type:
> countertest
> 
> If anyone has any suggestions or ideas to fix this BUG can you please e-mail
> me
> ASAP, any other help on how to avoid this bug until someone can come up with
> a patch would be very much appreciated.
> 
> 
> 
> Arash
> 
> 
> 
> 
> PS: The malloc in question can be found on line 114 of DigitList.cpp



---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
Org. 8920, MS 9012                  ejfried@ca.sandia.gov
PO Box 969                  http://herzberg.ca.sandia.gov
Livermore, CA 94550

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]