Pronlem with delete, Error: CrtIsValidHeapPointer

This is a discussion on Pronlem with delete, Error: CrtIsValidHeapPointer within the C++ Programming forums, part of the General Programming Boards category; I have a simple class: Code: class gog { private: char *s; int slen; public: gog (const char*s) { slen ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Pronlem with delete, Error: CrtIsValidHeapPointer

    I have a simple class:

    Code:
    class gog
    {
    private:
    	char *s;
    	int slen;
    
    public:
    
    	gog (const char*s) 
    	{
    		slen = strlen(s);
    		s = new char [slen];
    	}
    	
    	gog (int l = 4) : slen(l) 
    	{
    		s = new char [l];
    	}
    
    	~gog ()
    	{
    		if (s) 
    		{
    			if (slen < 2)
    				delete s;
    			else
    				delete[] s;
    		}
    	}
    };
    If I declare an instance of gog in main(), I always receive an assertion error whenever the program ends. So I checked out dbgheap.c and read:

    /*
    * If this ASSERT fails, a bad pointer has been passed in. It may be
    * totally bogus, or it may have been allocated from another heap.
    * The pointer MUST come from the 'local' heap.
    */

    about the specific assert.

    The code I'm using looks right. Is there something I'm forgetting? All I'm doing is creating an instance. If I use the default constructor or I pass in a string or anything it fails... If I comment out the deletes then it works fine. Argh! What's wrong?

    thanks for any help!
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    in both of your constructors u are are using the array brackets to allocate memory on the heap by writing:

    Code:
    s = new char [slen];
    
    and
    
    s = new char [l]
    and in the first case you will allocate an array and so will you in the second time b/c you type:

    Code:
    gog (int l = 4) : slen(l)
    and in this case when you call this constructor with some parameter value it will ignore it and allocate and array of 4 items b/c you wrote: int I = 4 (default value)

    so ........in your destructor you should just use:

    Code:
    delete [] s;
    try that.........
    I haven't tried your code, so I can't tell if that acutally causes an error, but try it and let me know...


    also .........what compiler are you using ???

    ...............
    matheo917

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    It's defaulted at four, even though four is just a random number, so it always allocates space, so I don't think that's the problem. Even if I remove the check to see if the size is less than 2 delete, it still causes an assert error when I delete the array no matter which constructor I use.

    I'm using MSVC++ 6 on Windows 98. =)

    thanks
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i thought so.....

    anyway.......i used to use vc++ 6.0, but that sucker had too many internal compiler errors so I switched to visual c++ .NET version...and a lot of things started to look a lot nicer...

    i ran your code on Visual C++ .NET and everything compiles and links fine, which leads me to believe that an error is initiated due to the compiler that u are using...........

    .........unless somebody else has better suggestions...???

    ..........
    matheo917

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    VC++ is one of the most widely used compilers out there, I don't think that it would choke on deleting a character array. Or maybe it would... meh =/

    I have to be doing something wrong.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    well..........

    maybe you want to post more code or all of it and we'll try to go over it again in a greater perspective and perhaps we'll see if my hypothesis will hold true........

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    After running your code a few times, I think I found an answer. The default constructor and the constructor taking an int as an argument worked fine. However, when I tried the const char * constructor, it failed. The problem seemed to be the naming of your argument.
    Code:
    class gog
    {
    private:
       char *s; //note the s here
       int slen;
    
    public:
    gog (const char*s) //note the s here
    {
       slen = strlen(s);
       s = new char [slen]; //which s is it using?
    }
    //your other code
    };
    When I changed your constructor to this:
    Code:
    gog (const char*a)
    {
       slen = strlen(a);
       s = new char [slen];
    }
    It worked fine. BTW, I'm using VC++ 6.0.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ps. what libraries did you include in your source code?

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Sorry to bump a slightly old topic, but I haven't been able to get to a computer for a while, and I still need help.

    Anyway, the name conflict was just coincidental and not the source of error.
    The only lib I'm using is <iostream>.

    Here is my complete code for a string-like class. It's got a lot of problems, I'm sure (which I'd love if anyone could help me fix), but mainly I want to be able to delete the contents... for if I remove any delete statements from the destructor, there are no errors = /

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    #define	MAX_ALLOWED_BYTES		4294967295
    
    class RMString
    {
    private:
    
    	char *intern;
    	int slen;
    	unsigned int i;
    
    public:
    
    	RMString (int len = 2) : slen(len) // intern is created with len elements
    	{
    		if (len <= MAX_ALLOWED_BYTES)
    		{
    			intern = new char [len];
    		}
    	}
    
    	RMString (const char *astring) // create intern with c-string
    	{
    		if (astring)
    		{
    			unsigned int l = strlen(astring);
    			if (l <= MAX_ALLOWED_BYTES)
    			{
    				slen = l;
    				
    				intern = new char [ slen ];
    
    				if (intern)
    				{
    					strcpy(intern,astring);
    				}
    			}
    		}
    	}
    
    	RMString (const RMString &rmstr) // create intern with rmstring
    	{
    		unsigned int l = rmstr.length();
    		if (l <= MAX_ALLOWED_BYTES)
    		{
    			intern = new char [l];
    
    			for (i = 0; i < l; ++i)
    			{
    				intern[i] = rmstr.value(i);
    			}
    			
    			intern[i] = '\0';
    			slen = l;
    		}
    	}
    
    	~RMString () // destructor
    	{
    		if (intern)
    		{
    			if (slen < 2)
    				delete intern;
    			else
    				delete[] intern;
    		}
    	}
    
    public:
    
    	inline unsigned int length (void) const { return slen; } // get length
    	inline unsigned int size (void) const { return slen; }
    	
    	inline 
    	unsigned int defined_size (void) // useless
    	{
    		for (char *ptr = intern, i = 0; *ptr != '\0'; ++ptr, ++i)
    			;
    
    		return i;
    	}
    
    	inline int last_index (void) const { return slen - 1; } // get the last place
    
    	inline char lastc (void) const { return *(intern + (slen - 1)); } // get last character
    	inline char firstc (void) const { return *(intern); } // get first character
    
    	inline
    	char value (unsigned int position) const // same as [] operator
    	{
    		if (position > slen)
    			return '^';
    		return *(intern + position);
    	}
    
    	inline
    	char value_nocheck (unsigned int position) const
    	{
    		return *(intern + position);
    	}
    
    	inline
    	const char * c_str (void) const // get c-style string
    	{
    		return intern;
    	}
    
    public:
    
    	inline
    	char operator [] (unsigned int position) const // get a value
    	{
    		if (position > slen)
    			return '^';
    		return *(intern + position);
    	}
    
    	inline
    	char& operator [] (unsigned int position) // set a value
    	{
    		return *(intern + position);
    	}
    
    	inline
    	bool operator == (const RMString &rmstr) // compare rmstrings
    	{
    		if (rmstr.size() != slen)
    			return false;
    
    		for (i = 0; i < slen; ++i)
    		{
    			if (*(intern + i) != rmstr.value_nocheck (i))
    				return false;
    		}
    
    		return true;
    	}
    
    	inline
    	bool operator != (const RMString &rmstr) // not equal
    	{
    		return !((*this) == rmstr);
    	}
    
    	RMString& operator = (const RMString &rmstr) // set equal to
    	{
    		unsigned int l = rmstr.length();
    		
    		if (intern)
    		{
    			delete[] intern;
    			intern = new char [l];
    			
    			for (i = 0; i < l; ++i)
    			{
    				intern[i] = rmstr.value_nocheck(i);
    			}
    			
    			intern[i] = '\0';
    			slen = l;
    		}
    
    		return (*this);
    	}
    };
    
    int main (void)
    {
    	RMString a ("Hello!");
    
    	cout << a.c_str() << endl;
    
    	return (0);
    }
    Last edited by rmullen3; 02-21-2003 at 09:50 PM.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  10. #10
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you say that your program has a lot of errors and you want someone to help you fix them...

    hmmm..... just for future reference ---> if in fact you post a larger amount of code it would help to have a lot of comments along with it, b/c otherwise i don't really think there's a lot of people out there who really want to spend a lot of time trying to read through your code and figure out (from scratch) what it is that you are trying to do ...............


    ps. anyway.....i see that you must be using more library files besides iostream..........like: cstring.............right???

  11. #11
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    OK, I (kind of) commented the code.

    Yeah, I was using <cstring> but didn't include it.. It didn't fix the problem.

    It may have something to do with strcpy, or with not null-terminated the strings, or some other error. But then, I get an error when deleting even a simple character array as shown in the example of my first post. Jeebus. =/

    thanks
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    OK, I think I've got it working, just one last question: should I set the last character to '\0' in all times I change the string, ie, in all constructors, the assignment operator, etc.?

    Thanks for everyone's help!
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  13. #13
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    most likely....................

    in reality when you utilize a (string) data type, that is defined in a <string> library, that string will have \0 as its last character

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 02:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21