On Sun, 14 Feb 2010, Tony Rick wrote:

> Date: Sun, 14 Feb 2010 13:37:42 -0800
> From: Tony Rick <tony...@gmail.com>
> Reply-To: "General Linux/UNIX discussion and help;    civil and on-topic"
>     <plug@lists.pdxlinux.org>
> To: "General Linux/UNIX discussion and help,  civil and on-topic"
>     <plug@lists.pdxlinux.org>
> Subject: Re: [PLUG] C++ pointer freeing problem
> 
> On Sun, Feb 14, 2010 at 12:59 PM, Tony Rick <tony...@gmail.com> wrote:
>
>>
>>
>> On Sun, Feb 14, 2010 at 10:46 AM, Carlos Konstanski <
>> ckonstan...@pippiandcarlos.com> wrote:
>>
>> What is the error message from Visual C++?
>>
>> What is the value of rawInput in the g++ version after the call returns?
>>
>> - tony
>>
>
> So I wrote a test program, using your function and g++:
>
> #include <string>
> #include <iostream>
> #include <stdio.h>
>
> int main(int argc, char **argv) {
>
>    string rawInput("1234567");
>    cout << rawInput << endl;
>    printf ("%s\n", rawInput.c_str());
>    bool rslt = numericP(rawInput);
>    cout << rawInput << endl;
> }
>
>
> Of course,  I have no idea what your calling code looks like, so I went
> minimal.  As you said,  g++ likes it just fine, and rawInput is intact upon
> return.
>
> Please give us a code example of what you mean when you say 'But later
> attempts to use the value that I passed in as rawInput'.
>
> - tony

OK, I put up a Visual C++ zip file at:

   http://mirror.wrlug.org/code-samples/3trianglesAndDiamonds.win32.zip

Naturally my real-life code is not exactly the same as what I posted
earlier. I was trying to avoid (what I thought to be) undue
complexity. I don't really pass in any arguments; it's a method that
acts upon other class members. The treatment of the input string needs
to be examined in the proper context.

If you look in Validate.cpp, you will see the following two methods:



bool Validator::numericP() {
     bool legalCharsP = true;
     int numDecimalPoints = 0;
     int i = 0;
     const char *charSequence = getRawInput().c_str();
     while(i < getRawInput().size() && legalCharsP && numDecimalPoints <= 1) {
         // the first char can be a -, ., or a digit; all others must
         // be a . or a digit; but there can be only one or less .'s
         if(! ((i == 0 && charSequence[i] == 0x2d) ||
               ((charSequence[i] >= 0x30 && charSequence[i] <= 0x39) || 
charSequence[i] == 0x2e))) {
             legalCharsP = false;
         } else if(charSequence[i] == 0x2e) {
             numDecimalPoints++;
         }
         i++;
     }
     return (legalCharsP && numDecimalPoints <= 1);
}

int Validator::validate() {
     int num = 0;
     // DEBUG: this if() statement, when run in Windows, causes
     // some sort of curruption of rawInput, as evidenced by the
     // fact that calling getRawInput() fails with a NaNException
     if(! numericP()) {
         throw NaNException();
     }
     // this throws a NaNException in Windows
     cout << getRawInput() << endl;
     coerceStringToNumeric(getRawInput(), num);
     if(num < 1 || num > 80) {
         throw OutOfRangeException();
     }
     return num;
}



After the call to numericP(), any attempt to call getRawInput() throws
the exception. I put in a super-simple, gratuitous cout to illustrate
this. And here is the error message:

Unhandled exception at 0x7642b727 in 3trianglesAndDiamonds.exe:
Microsoft C++ exception: NaNException at memory location 0x0035fa00..

Which occurs on the line:

   cout << getRawInput() << endl;

Commenting out the "if(! numericP()) {}" block circumvents the
error. Therefore it seems reasonable that something in numericP() is
causing the trouble. That's what led me to look at the treatment of
the *char. Now I think it's corrupting rawInput().

Is it bad to keep using getRawInput() directly whenever I want to read
its value, instead of getting a copy into a local variable? This would
cause no problems in Java, Lisp, etc, but perhaps C++ is
different. I'll try changing that.

I appreciate all your help thus far. It is good to know that I should
not bark up the free() tree.

Carlos
_______________________________________________
PLUG mailing list
PLUG@lists.pdxlinux.org
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to