Debug Assertion Failed

Jul 10, 2016 at 1:56am
Hi everybody,

I have some class in my program.
The class has a method which iterates over an array in order to find some int info.
When it finish the job, it saves the int to a local variable.
But when the program tries to return that variable, suddenly a run-time error occurs and the following message is displayed:

Debug Assertion Failed!
Program: ...a path to my program...
File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how you program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)


Does somebody can explain what's the problem?
Also It may be important to know that if instead of calling that function I just write it's code there, everything works fine.
Last edited on Jul 10, 2016 at 2:21am
Jul 10, 2016 at 1:58am
Can you post the program?
Jul 10, 2016 at 2:14am
It's very long...
Also It may be important to know that if instead of calling that function I just write it's code there, everything works fine. Pretty weird....
Last edited on Jul 10, 2016 at 2:17am
Jul 10, 2016 at 2:50am
Can you at least post the relevant portion of the program then?
Jul 10, 2016 at 3:36am
The call to the function:
 
int fcbIndex = findFCB(dms);

The definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static int findFCB(DMS dms)
{
	int Index = -1, choice = 1;
	cout << "Please Select A File From The Following:" << endl;
	for (int i = 0; i < dms._fcbArrSize; i++)
	{
		if (dms._fcbArray[i]._d != NULL)
		{
			cout << choice << " - " << dms._fcbArray[i]._fileDesc._fileName << endl;
			choice++;
		}
	}
	cout << endl;
	cin >> Index;
	choice = 1;
	for (int i = 0; i < dms._fcbArrSize; i++)
	{
		if (choice == Index)
		{
			Index = i;
			break;
		}
		if (dms._fcbArray[i]._d != NULL)
			choice++;
	}
	return Index;
}


Note: I've tried also without the static keyword, it's not the problem.
Last edited on Jul 10, 2016 at 3:38am
Jul 10, 2016 at 7:37am
Can the DMS class handle being copied?
Jul 10, 2016 at 10:50am
What do you mean? Do you want me to copy the class definition to here?
Last edited on Jul 10, 2016 at 11:18am
Jul 10, 2016 at 1:05pm
I mean you are passing DMS by value to the function which will create a copy. I was just thinking that maybe it can't handle being copied. See the rule of three: https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

You post the class definition here if you want us to take a look at it. It's up to you.
Jul 10, 2016 at 2:19pm
Yes, this was exactly the problem! Thank you!

Can you explain more precisely why passing by value causes the problem?
Jul 11, 2016 at 4:57am
A common problem is when you have a class that has a pointer to some dynamically allocated memory without defining a copy constructor and a copy assignment operator. What happens when you copy such a class is that you end up with two objects with their pointers pointing to the same memory. When one of the objects are destroyed (e.g. dms is destroyed at the end of findFCB) the destructor will deallocate the memory that the pointers pointing to, leaving the other object pointing to memory that is no longer available.
Topic archived. No new replies allowed.