locked
VS 2010 64bit CString destructor/release error RRS feed

  • Question

  • Hello,

    Facing an CString destructor / release error issue, on executing 64bit Application on 64bit Win 2008 server.

    Same when the 32bit application executed on Win 2008 64bit server is working fine.

    From the Debug I noticed, initialize is success and on release this -->as bad pointer.

    Bad Pointer, on release

    To confirm I tried 64bit Cstring sample exactly the same as our application

     only thing CString value is assigned manually & which is working fine,

    #include <iostream>
    #include <afx.h>
    
    using namespace::std;
    
    int CallString1 ( BYTE* Val )
    {
      cout <<Val <<endl;
      return 0;
    }
    
    int CallString2 ( CString Val )
    {
       int a = CallString1 ( (BYTE*) (const char* ) Val );
       return a;
    }
    
    int main ()
    {
     CString a = "ab";
     int b = CallString2( a );
    
     return 0;
    }

    Thanks,

    John


    Johnson

    Thursday, October 1, 2015 1:18 AM

Answers

  • Hi John,

    Does the demo code build without any error on your side?

    > int a = CallString1 ( (BYTE*) (const char* ) Val );

    Which compiler do you use? How did you convert CString to const char *?

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Edited by May Wang - MSFT Thursday, October 1, 2015 6:54 AM
    • Marked as answer by John_IN Tuesday, October 6, 2015 8:03 AM
    Thursday, October 1, 2015 6:31 AM
  • I assume that your project is not using Unicode character set?

    I don't think this can be the cause of the problem (if this is indeed your full code), but your code should be using const BYTE*, not BYTE*:

    int CallString1 ( const BYTE* Val )
    {
      cout << Val << endl;
      return 0;
    }
    
    int CallString2 ( CString Val )
    {
      int a = CallString1 ( (const BYTE*) (const char* ) Val );
      return a;
    }
    

    If you were to modify your BYTE* array, then you could indeed corrupt the CString object.

    Also, why you are not passing your CString by reference?

    int CallString2 ( const CString& Val )
    {
      int a = CallString1 ( (const BYTE*) (const char* ) Val );
      return a;
    }
    

    Does making this change get rid of the error?


    David Wilkinson | Visual C++ MVP

    • Edited by davewilk Thursday, October 1, 2015 8:56 AM
    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Thursday, October 1, 2015 8:47 AM
  • Hi May,

    Yes, Demo build successfully and also program executed without any error.

    1. Frist I tried only with CString option, to check if it is working fine.

            Result is success.

    2. Later I changed the code as how it is implemented in our application, like

       a. First function passes CString

       b. then called function does implicit conversion to BYTE* --> and passes to next function.

       c. Application is not crashing when the implicit conversion or called to that function.

       d. Application crashes at First Function were CString is refered,close or return.

    This happens only for 64bit VS 2010 VC++ at runtime error.

    The computer I have here has only VS2008. I do not get any runtime error on x64 debug build.

    David Wilkinson | Visual C++ MVP

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Thursday, October 1, 2015 8:58 AM
  • In addition to David W's suggestions, I'd encourage you to use C++ style casts (e.g. static_cast, reinterpret_cast, etc.) instead of those old C-style casts.

    Anyway, I built your code with VS2010 in 64-bit mode (assuming CStringA for CString), and I didn't get the errors you mentioned.

    Please provide a compilable repro code snippet.

    Moreover, you may get better help if you try to clarify your intentions. What is your code actually trying to do? What kind of problem are you trying to solve?

    Giovanni

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Thursday, October 1, 2015 3:53 PM
  • Johnson,

    Using "const" for parameters is always a good plan when your function will not attempt to modify the data.  And if you are passing the pointer to the character string within a CString, you had better not be trying to modify that data!

    Your problem description sounds to me like something has been corrupted.  Frankly, the idea of casting the contents of a CString as a BYTE * (or even const BYTE *) sounds fishy to me.  What are you doing with the BYTE * after you get it?


    Developer Support Engineer

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Monday, October 5, 2015 9:52 PM
  • Yes, even I was bit confusing here because code was written in early 1992's and currently I am migrating source to 64bit to deploy on Win 2008 server.

    I replaced CString to char* and try to resolve some of the runtime issues.

    Looks fine until, let me proceed further.

    Thanks all for your help.

    Cheers,

    John


    Johnson

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    • Unmarked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    • Marked as answer by John_IN Wednesday, October 7, 2015 7:45 AM
    Tuesday, October 6, 2015 8:02 AM

All replies

  • Hi John,

    Does the demo code build without any error on your side?

    > int a = CallString1 ( (BYTE*) (const char* ) Val );

    Which compiler do you use? How did you convert CString to const char *?

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Edited by May Wang - MSFT Thursday, October 1, 2015 6:54 AM
    • Marked as answer by John_IN Tuesday, October 6, 2015 8:03 AM
    Thursday, October 1, 2015 6:31 AM
  • Hi May,

    Yes, Demo build successfully and also program executed without any error.

    1. Frist I tried only with CString option, to check if it is working fine.

            Result is success.

    2. Later I changed the code as how it is implemented in our application, like

       a. First function passes CString

       b. then called function does implicit conversion to BYTE* --> and passes to next function.

       c. Application is not crashing when the implicit conversion or called to that function.

       d. Application crashes at First Function were CString is refered,close or return.

    This happens only for 64bit VS 2010 VC++ at runtime error.

     Thanks,

    John


    Johnson

    Thursday, October 1, 2015 7:30 AM
  • I assume that your project is not using Unicode character set?

    I don't think this can be the cause of the problem (if this is indeed your full code), but your code should be using const BYTE*, not BYTE*:

    int CallString1 ( const BYTE* Val )
    {
      cout << Val << endl;
      return 0;
    }
    
    int CallString2 ( CString Val )
    {
      int a = CallString1 ( (const BYTE*) (const char* ) Val );
      return a;
    }
    

    If you were to modify your BYTE* array, then you could indeed corrupt the CString object.

    Also, why you are not passing your CString by reference?

    int CallString2 ( const CString& Val )
    {
      int a = CallString1 ( (const BYTE*) (const char* ) Val );
      return a;
    }
    

    Does making this change get rid of the error?


    David Wilkinson | Visual C++ MVP

    • Edited by davewilk Thursday, October 1, 2015 8:56 AM
    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Thursday, October 1, 2015 8:47 AM
  • Hi May,

    Yes, Demo build successfully and also program executed without any error.

    1. Frist I tried only with CString option, to check if it is working fine.

            Result is success.

    2. Later I changed the code as how it is implemented in our application, like

       a. First function passes CString

       b. then called function does implicit conversion to BYTE* --> and passes to next function.

       c. Application is not crashing when the implicit conversion or called to that function.

       d. Application crashes at First Function were CString is refered,close or return.

    This happens only for 64bit VS 2010 VC++ at runtime error.

    The computer I have here has only VS2008. I do not get any runtime error on x64 debug build.

    David Wilkinson | Visual C++ MVP

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Thursday, October 1, 2015 8:58 AM
  • In addition to David W's suggestions, I'd encourage you to use C++ style casts (e.g. static_cast, reinterpret_cast, etc.) instead of those old C-style casts.

    Anyway, I built your code with VS2010 in 64-bit mode (assuming CStringA for CString), and I didn't get the errors you mentioned.

    Please provide a compilable repro code snippet.

    Moreover, you may get better help if you try to clarify your intentions. What is your code actually trying to do? What kind of problem are you trying to solve?

    Giovanni

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Thursday, October 1, 2015 3:53 PM
  • Hello David/Giovanni,

    Yes the sample 64bit is working fine.

    At Application code level I did multiple changes to test,

    1. Replaced CString to string; //some functions worked fine.

    2. For some functions, initially it worked fine with CString type itself later application started crashing. (at Destructor level, again from beginning....)

    3. I tried to change the code CString to CString& reference, no luck

    4. also I tried to convert CString to BYTE..

    5. In my application code data types are BYTE* not const BYTE* 

    In the code, there are four places CString is used...

    Example

    func( CString Val )
    {
    
    ....
    
    CString FileName = GetProfileString(section,home,drv)
    //where section --> static char BASED_CODE section[] = "ABC"
    
    //home --> static char BASED_CODE home[] = "ABC_home"
    //drv --> static char BASED_CODE drv[] ="C:\ABC\";
    
    FileName += "\\" + FileBaseName;
    
    //where FileBaseName --> static char BASED_CODE FileBaseName[] = "File.txt"
    
    CString ver(Rev);
    
    .........
    ...................
    CString ErrMsg ; ErrMsg ="error handle"
    
    ............
    ............
    
    }

    So this Cstring variables are converted to BYTE* using...

    func1 ( (BYTE*) (const char* ) filename )

    so on...


    Johnson

    Monday, October 5, 2015 8:21 AM
  • Johnson,

    Using "const" for parameters is always a good plan when your function will not attempt to modify the data.  And if you are passing the pointer to the character string within a CString, you had better not be trying to modify that data!

    Your problem description sounds to me like something has been corrupted.  Frankly, the idea of casting the contents of a CString as a BYTE * (or even const BYTE *) sounds fishy to me.  What are you doing with the BYTE * after you get it?


    Developer Support Engineer

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    Monday, October 5, 2015 9:52 PM
  • Yes, even I was bit confusing here because code was written in early 1992's and currently I am migrating source to 64bit to deploy on Win 2008 server.

    I replaced CString to char* and try to resolve some of the runtime issues.

    Looks fine until, let me proceed further.

    Thanks all for your help.

    Cheers,

    John


    Johnson

    • Marked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    • Unmarked as answer by John_IN Tuesday, October 6, 2015 8:02 AM
    • Marked as answer by John_IN Wednesday, October 7, 2015 7:45 AM
    Tuesday, October 6, 2015 8:02 AM