none
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Question

    Question

  • I have the following class TestString. I am trying two different ways of using the class, one shown with DEBUG defined, and one shown without DEBUG defined. The code runs fine if DEBUG is not defined, but I am not sure why I get an _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) assert failure when DEBUG is defined. The assert failure happens when all the printfs have displayed their results and before _tmain terminates, which is likely when the destructor of TestString is called. I am under the impression that both ways of using the class is the same. Can someone please let me know?

    #include "stdafx.h"
    #include <stdio.h>
    #include <wchar.h>

    //#define DEBUG

    class TestString {
    public:
        char *string;
        unsigned int size;
       
        TestString(unsigned int _size)  
        {
            string = new char[_size];
            if (string != NULL)
            {
               size = _size;
            }
            else
            {
               size = 0;
            }
        }

        ~TestString()
        {
            delete[] string;
        }
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    #ifdef DEBUG
        TestString test(100);

        test.string = "This is a test1";

        printf("Text1: %s\n", test.string);
        printf("Size1: %d\n", test.size);

        printf("End of Test1\n");
     
    #else
        TestString *test = new TestString(100);

        test->string = "This is a test2";

        printf("Text2: %s\n", test->string);
        printf("Size2: %d\n", test->size);

        printf("End of Test2\n");
    #endif

        return 0;
    }

     

    • Edited by l8chen Wednesday, June 03, 2009 7:39 PM
    Wednesday, June 03, 2009 7:36 PM

Answers

  • The primary difference here is that, when you NEW an object (in this case, your TestString class), you're responsible for destructing it.   So the DEBUG version has the destructor called on test...but the runtime version does not.   That's where your problem lies.

    But why is your destructor failing?  Because you allocate a ptr to memory...then *change* that pointer when you set it to a static string.   You're trying to free memory you didn't allocate.  

    The basic problem is that, for a string class to operate properly, you need to overload the assignment operator.  To get proper value semantics, though, this can get complicated fast...a good string class is pretty complicated.
    Michael Asher
    • Marked as answer by l8chen Wednesday, June 03, 2009 8:58 PM
    Wednesday, June 03, 2009 7:53 PM

All replies

  • When you assign a const literal to the string member, then calling delete[] on it is not valid. You need to add a SetString method that will copy the string to your internal buffer.
    http://blog.voidnish.com
    Wednesday, June 03, 2009 7:43 PM
  • But why is it OK when DEBUG is not defined?
    Wednesday, June 03, 2009 7:51 PM
  • The primary difference here is that, when you NEW an object (in this case, your TestString class), you're responsible for destructing it.   So the DEBUG version has the destructor called on test...but the runtime version does not.   That's where your problem lies.

    But why is your destructor failing?  Because you allocate a ptr to memory...then *change* that pointer when you set it to a static string.   You're trying to free memory you didn't allocate.  

    The basic problem is that, for a string class to operate properly, you need to overload the assignment operator.  To get proper value semantics, though, this can get complicated fast...a good string class is pretty complicated.
    Michael Asher
    • Marked as answer by l8chen Wednesday, June 03, 2009 8:58 PM
    Wednesday, June 03, 2009 7:53 PM
  • The Release build will happily let you leak and corrupt memory, as your program as doing, without complaining about it.  It doesn't usually last long in a real program before Windows puts a stop to that.

    Hans Passant.
    Wednesday, June 03, 2009 8:05 PM
  • Ok thanks for your explanation. I will look into this.
    Wednesday, June 03, 2009 8:59 PM