Discussion:
deleting "items" pointer - errors
(too old to reply)
Jacob Fussek
2004-05-25 22:24:45 UTC
Permalink
When I put the line
"delete items;"
in my removeAll() method, I get the following error when I run the
compiled file:

"Microsoft Visual C++ Debug Library
Debug Assertion Failed!

Program:...
File: dbgheap.c
line: 1044

Expression: _CrtIsValidHeapPointer(pUserData)

See Visual c++ docs for more information on how your program can cause
such an assertion failure."

Note that putting the same line in my constructor method for
listOfIntegers doesn't bring up that error.

I have absolutely no idea what the problem is... Any ideas?

Jacob
CS 246 Tutor
2004-05-25 22:46:32 UTC
Permalink
Without knowing more about your code, it's very difficult to say.

Given that the error contains the text "IsValidHeapPointer", my guess is
you're either trying to use items again after it's been deleted but before
it's been reassigned, or you're deleting items more than once.

Terry
Post by Jacob Fussek
When I put the line
"delete items;"
in my removeAll() method, I get the following error when I run the
"Microsoft Visual C++ Debug Library
Debug Assertion Failed!
Program:...
File: dbgheap.c
line: 1044
Expression: _CrtIsValidHeapPointer(pUserData)
See Visual c++ docs for more information on how your program can cause
such an assertion failure."
Note that putting the same line in my constructor method for
listOfIntegers doesn't bring up that error.
I have absolutely no idea what the problem is... Any ideas?
Jacob
Jacob Fussek
2004-05-25 23:04:44 UTC
Permalink
right after the
"delete items;"
line, I have the following line:
"items = new Items[0];"
which should reassign the items pointer to the new Items array. Therefore,
I don't think I'm deleting an item more than once, cause items is assigned
to a new item again. As well, I am not using the item after it's been
deleted cause again, I'm pointing to a new item.
If it helps, I'm also reassigning numOfItems to 0 in that method so that I
am keeping track of the size correctly.

I can show you the code at the next tutorial if that'll help...
Post by CS 246 Tutor
Without knowing more about your code, it's very difficult to say.
Given that the error contains the text "IsValidHeapPointer", my guess is
you're either trying to use items again after it's been deleted but before
it's been reassigned, or you're deleting items more than once.
Terry
Post by Jacob Fussek
When I put the line
"delete items;"
in my removeAll() method, I get the following error when I run the
"Microsoft Visual C++ Debug Library
Debug Assertion Failed!
Program:...
File: dbgheap.c
line: 1044
Expression: _CrtIsValidHeapPointer(pUserData)
See Visual c++ docs for more information on how your program can cause
such an assertion failure."
Note that putting the same line in my constructor method for
listOfIntegers doesn't bring up that error.
I have absolutely no idea what the problem is... Any ideas?
Jacob
CS 246 Tutor
2004-05-25 23:32:00 UTC
Permalink
Post by Jacob Fussek
right after the
"delete items;"
"items = new Items[0];"
This must not be in your submitted code, since the only line containing
"delete items;" in either arrayList.cc or linkedList.cc is:

ListOfIntegers::~ListOfIntegers() {delete items;}
Post by Jacob Fussek
I can show you the code at the next tutorial if that'll help...
Come to my office hours some time later this week. And remind me to post
to the newsgroup what the error indicated (as well as what the
corresponding g++ error is).

Terry
Ian Halliday
2004-05-26 17:51:06 UTC
Permalink
Too late for the assignment, but I just want to point out that a problem
with this is that in this case items appears to be used as an array of
Items, which means it should be deleted with the array deletion operator
'delete []'. I.e. delete [] items;

Second, creating an array on the heap of size zero is just plain silly.
After deleting an array or object, its best to set the pointer to null if
the pointer will be used again later. Note that C++ allows null pointers
to be deleted, and this results in nothing.

And a question to the tutor, should we use the old C NULL macro for null
in our C++ programs, or just 0? I've been told to use 0 in C++ instead of
NULL because NULL isn't a standard macro in C++ and can cause issues and
what not. Being a course about software engineering I figure
which should conform to strict guidelines, and so I'm guessing using 0
instead of NULL is a good idea. But does it really matter?

Ian Halliday
Post by CS 246 Tutor
Post by Jacob Fussek
right after the
"delete items;"
"items = new Items[0];"
This must not be in your submitted code, since the only line containing
ListOfIntegers::~ListOfIntegers() {delete items;}
Post by Jacob Fussek
I can show you the code at the next tutorial if that'll help...
Come to my office hours some time later this week. And remind me to post
to the newsgroup what the error indicated (as well as what the
corresponding g++ error is).
Terry
CS 246 Tutor
2004-05-26 21:17:24 UTC
Permalink
Post by Ian Halliday
And a question to the tutor, should we use the old C NULL macro for null
in our C++ programs, or just 0? I've been told to use 0 in C++ instead of
NULL because NULL isn't a standard macro in C++ and can cause issues and
what not. Being a course about software engineering I figure
which should conform to strict guidelines, and so I'm guessing using 0
instead of NULL is a good idea. But does it really matter?
As you said, NULL isn't automatically defined, as you'll find out if you
try to compile a piece of code that doesn't #include a system header file.
However, there are 2 very good reasons to use NULL:

1) It provides a nice abstraction away from what's really happening behind
the scenes.

2) NULL is not guaranteed to be defined to 0. Some functions have a
possible return value of NULL, and this may cause your code to fail if you
check it against 0.

Terry

Loading...