Error Debug Assertion Failed!

Pages: 123
Hello,

I'm working with a Visual Studio 2015 C++ program converted from a VS 6.0 C++ program.

When I run the program it gives this error:


---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: C:\WINDOWS\SYSTEM32\mfc140d.dll
File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\array_u.cpp
Line: 324

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

(Press Retry to debug the application)

---------------------------
Abort   Retry   Ignore   
---------------------------


It's giving this error as a user message. It is not stopping in the debugger showing a line where the error is occurring.

I used the debugger to find out where it is blowing up. It is stopping inside this for loop that is in the Carray::SetSize method found in the afxtempl.h file:

1
2
for( int i = 0; i < m_nSize-nNewSize; i++ )
	(m_pData + nNewSize + i)->~TYPE();


For some reason, when I first started to get this error, it was a different error message and it stopped in the debugger. I tried to get help to fix it here:

I never did figure out how to fix it, so I am hoping that someone can help in figuring out what needs to be done.

Any help that anyone can provide to resolve this error would be gratefully appreciated.

Thanks,
Tony
Last edited on
Can you show us the code of the program - especially before the failde assertion
Hello Thomas.

Thanks for your help.

I'm not sure of how far back to go, but in this method, a call is made to Serialize.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
BOOL CMagicDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	LPBYTE pData = NULL;
	BOOL bOK = TRUE;
	CWaitCursor wait;
	try
	{
		CFile file;
		CFileException fe;
		if (!file.Open(lpszPathName, CFile::modeRead, &fe))
		{
			ReportSaveLoadException(lpszPathName, &fe, 
				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
			return FALSE;
		}
		DeleteContents();
		SetModifiedFlag();  // dirty during de-serialize

		DWORD dwLen = file.GetLength();
		pData = new BYTE[dwLen+5];
		DWORD dwRead = file.Read(pData, dwLen);

		// check signiture - encrypted?
		if (IsEncrypted(pData, dwRead))		// decrypt data
		{
			// prompt for password
			do 
			{
				DPassword dlg(FALSE);
				if (dlg.DoModal() != IDOK)
				{
					delete pData;
					SetModifiedFlag(FALSE);
					// can not return FALSE, due to bugs in reinit
					if (AfxGetMainWnd())
						AfxGetMainWnd()->PostMessage(WM_COMMAND, ID_FILE_NEW);
					else
						((CMagicApp*)AfxGetApp())->m_bReset = TRUE;
					return TRUE;
				}
				m_sPassword = dlg.m_sPassword;
			}
			while (!DecryptData(pData, dwRead, m_sPassword));
		}
		else
			m_sPassword.Empty();

		CMemFile mf;
		mf.Attach(pData, dwRead);
		CArchive ar(&mf, CArchive::load);
		ar.m_pDocument = this;
		Serialize(ar);     // load me
		ar.Close();
		mf.Detach();
	}
	catch (CException* e)
	{
		ReportSaveLoadException(lpszPathName, e, 
				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		e->Delete();
		bOK = FALSE;
	}
	catch(...)
	{
		ReportSaveLoadException(lpszPathName, NULL, 
				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		bOK = FALSE;
	}
	delete pData;
	if (!bOK)
	{
		DeleteContents();
		return FALSE;
	}
	
	SetModifiedFlag(FALSE);
	
	CMagicFrame *wnd = (CMagicFrame*) AfxGetMainWnd();
	if( wnd )
	{
		wnd->UpdateMailCount( 0, 0 );
	}

	return TRUE;
}

This is the Serialize method:

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
28
29
void CMagicDoc::Serialize(CArchive& ar)
{
	m_listMailbox.Serialize( ar );
	if (ar.IsStoring())
	{
		ar << nVersion;
		theApp.m_Filters.Serialize( ar );
	}
	else
	{
		// old version does not have version number stored, so
		//  have to use exception handler
		try
		{
			BYTE bVer = 0;
			ar >> bVer;
			if (bVer <= nVersion)
			{
				theApp.m_Filters.Serialize( ar );
			}
		}
		catch(CArchiveException* e)
		{
			e->Delete();
			theApp.GenDefaultFilters();
		}
	}
}


This method gets to the "try", and throws the exception. Here is what it calls in the exception processing:

1
2
3
4
5
6
7
8
9
10
11
12
13
void CMagicApp::GenDefaultFilters()
{
	m_Filters.SetSize(1);
	CMailFilter filter;
	filter.m_sName		= "Blacklist";
	filter.m_aCnd[0].m_nField		= MFF_FROM;
	filter.m_aCnd[0].m_nOperation = MFO_EQUAL;
	filter.m_aCnd[0].m_sText		= "$blacklst.txt";	// get from file
	filter.m_nCombination = MFC_NONE;
	filter.m_dwAction	= MFA_SPAM;
	filter.m_sMailBox	= "*";				// for all mailboxes
	m_Filters.SetAt(0, filter);
}


This is where it tries to do SetSize and gives the error.

Tony
Last edited on
You could try
1
2
3
   // m_Filters.SetSize(1);
   // m_Filters.SetAt(0, filter);
   m_Filters.Add(filter)

If I modify that method to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CMagicApp::GenDefaultFilters()
{
	//m_Filters.SetSize(1);
	CMailFilter filter;
	filter.m_sName		= "Blacklist";
	filter.m_aCnd[0].m_nField		= MFF_FROM;
	filter.m_aCnd[0].m_nOperation = MFO_EQUAL;
	filter.m_aCnd[0].m_sText		= "$blacklst.txt";	// get from file
	filter.m_nCombination = MFC_NONE;
	filter.m_dwAction	= MFA_SPAM;
	filter.m_sMailBox	= "*";				// for all mailboxes
	//m_Filters.SetAt(0, filter);
	m_Filters.Add(filter);
}


It does get to the "m_Filters.Add(filter);" line, but it gives this error:

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: C:\WINDOWS\SYSTEM32\mfc140d.dll
File: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxcoll.inl
Line: 213

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

(Press Retry to debug the application)

---------------------------
Abort   Retry   Ignore   
---------------------------


Thanks,
Tony
Last edited on
How are m_Filters and CMailFilter declared ?
This is the declaration of m_Filters

CArray<CMailFilter, CMailFilter&> m_Filters;

I'm not sure if a struct is a declaration, but this is what it looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct CMailFilter
{
	CString	m_sName;		// filter text
	
	enum {nConditions = 2};
	CFilterCnd	m_aCnd[nConditions];
	BYTE		m_nCombination;	// MFC_x

	CString	m_sMailBox;
	COLORREF m_Color;		// if color is used
	DWORD	m_dwAction;		// combination of MFA_*

	CMailFilter();
	CMailFilter(const CMailFilter& a);
	// CArray support
	CMailFilter& operator=(const CMailFilter&);
	void Serialize(CArchive& a);
};


Thanks,
Tony
Last edited on
Can you post the rest of CMailFilter? I try to test it in a simple Console app.
Thomas,

I'm not sure of what you mean by the rest of CMailFilter. If I do a find, there are 35 lines that have CMailFilter. Here are three methods for that name:
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
28
29
30
31
32
33
34
35
36
37
38
39

CMailFilter::CMailFilter()
{
	for (int i=0; i<nConditions; i++)
	{
		m_aCnd[i].m_nField = MFF_FROM;
		m_aCnd[i].m_nOperation = MFO_EQUAL;
	}
	m_nCombination = MFC_NONE;
	m_dwAction = MFA_NONE;
	m_Color = 0;	// black
	m_sMailBox = "*";
}


CMailFilter::CMailFilter(const CMailFilter& a)
{
	*this = a;
}


CMailFilter& CMailFilter::operator=(const CMailFilter& src)
{
	m_sMailBox = src.m_sMailBox;
	m_Color = src.m_Color;
	m_dwAction = src.m_dwAction;
	m_sName = src.m_sName;
	
	m_nCombination = src.m_nCombination;
	for (int i=0; i<nConditions; i++)
	{
		m_aCnd[i].m_nField = src.m_aCnd[i].m_nField;
		m_aCnd[i].m_nOperation = src.m_aCnd[i].m_nOperation;
		m_aCnd[i].m_sText = src.m_aCnd[i].m_sText;
	}

	return *this;
}


Thanks,
Tony
Still no obvious error to see.
Does it make a difference when you use a pointer?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CArray<CMailFilter*> m_Filters;

void CMagicApp::GenDefaultFilters()
{

	CMailFilter *filter = new CMainFilter;
	filter->m_sName		= "Blacklist";
	filter->m_aCnd[0].m_nField		= MFF_FROM;
	filter->m_aCnd[0].m_nOperation = MFO_EQUAL;
	filter->m_aCnd[0].m_sText		= "$blacklst.txt";	// get from file
	filter->m_nCombination = MFC_NONE;
	filter->m_dwAction	= MFA_SPAM;
	filter->m_sMailBox	= "*";				// for all mailboxes
	
	m_Filters.Add(filter);
}

Thomas,

If I try to use the pointer method, I get four compile errors.

Should I try to adjust the code errors to use the pointer? I don't even know what to do to fix the errors.

Here are the errors:


Severity	Code	Description	Project	File	Line	Suppression State
Error	C2440	'initializing': cannot convert from 'CMailFilter *' to 'CMailFilter &'	Magic	C:\Users\Tony\Documents\Visual Studio 2015\Projects\Magic2015\MagicFrame.cpp	1076	


Severity	Code	Description	Project	File	Line	Suppression State
Error	C2440	'initializing': cannot convert from 'CMailFilter *' to 'CMailFilter &'	Magic	C:\Users\Tony\Documents\Visual Studio 2015\Projects\Magic2015\Excerpt.cpp	566	


Severity	Code	Description	Project	File	Line	Suppression State
Error	C2664	'void CArray<CMailFilter *,const TYPE &>::Copy(const CArray<TYPE,const TYPE &> &)': cannot convert argument 1 from 'CArray<CMailFilter,CMailFilter &>' to 'const CArray<CMailFilter *,const TYPE &> &'	Magic	C:\Users\Tony\Documents\Visual Studio 2015\Projects\Magic2015\DFilters.cpp	299	


Severity	Code	Description	Project	File	Line	Suppression State
Error	C2664	'void CArray<CMailFilter,CMailFilter &>::Copy(const CArray<CMailFilter,CMailFilter &> &)': cannot convert argument 1 from 'CArray<CMailFilter *,const TYPE &>' to 'const CArray<CMailFilter,CMailFilter &> &'	Magic	C:\Users\Tony\Documents\Visual Studio 2015\Projects\Magic2015\DFilters.cpp	313	


Here are the four code areas where the errors occur:

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
28
29
void CMagicFrame::UpdateFilterLists()
{
	if (m_bListReady)
		return;
	m_asFilterLists.RemoveAll();
	for (int i=0; i<theApp.m_Filters.GetSize(); i++)
	{
		CMailFilter& mf = theApp.m_Filters.ElementAt(i);
		for (int c=0; c<CMailFilter::nConditions; c++)
		{
			if (mf.m_aCnd[c].m_sText.Find('$') != 0)
				continue;
			CString sFile = mf.m_aCnd[c].m_sText.Mid(1);
			sFile.MakeLower();
			sFile.TrimRight();
			for (int j=0; j<m_asFilterLists.GetSize(); j++)
			{
				if (m_asFilterLists[j] == sFile)
				{
					sFile.Empty();
					break;
				}
			}
			if (!sFile.IsEmpty())
				m_asFilterLists.Add(sFile);
		}
	}
	m_bListReady = TRUE;
}


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
28
void CExcerpt::CheckByFilters()
{
	if (IsFromFriend())
	{
		m_sFilters = _T("From friend!");
		m_dwFiltered = MFA_FRIEND;
		if (theApp.m_dwFlags & MMF_PROTECT)
		{
			m_bitRemoveFromServer = 0;
			m_bitProtected = 1;
		}
		return;
	}

	int nFilters = theApp.m_Filters.GetSize();
	DWORD dwLine = 0;
	for (int i=0; i<nFilters; i++)
	{
		CMailFilter& mf = theApp.m_Filters.ElementAt(i);
		if ((mf.m_dwAction & MFA_ENABLED)==0)
			continue;
		if ( !CheckByFilter(mf, dwLine) )
			continue;
		ApplyFilterAction(mf, dwLine);
		if ((mf.m_dwAction & MFA_OTHER)==0 || (mf.m_dwAction & MFA_FRIEND))
			break;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
void DFilters::OnOK() 
{
	UpdateData();
	theApp.m_Filters.Copy(m_Filters);
	
	theApp.intEnableFilters = m_bAllow;

	if (m_pDoc)
		m_pDoc->SetModifiedFlag();

	CDialog::OnOK();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void DFilters::GetData()
{
	m_bAllow = theApp.intEnableFilters;

	m_Filters.Copy(theApp.m_Filters);
	m_lbList.ResetContent();
	for (int i=0; i<m_Filters.GetSize(); i++)
	{
		CMailFilter& mf = m_Filters.ElementAt(i);
		int idx = m_lbList.AddString(mf.m_sName);
		m_lbList.SetCheck(idx, (mf.m_dwAction & MFA_ENABLED)!=0 );
	}
	m_lbList.SetCurSel(0);
	UpdateData(FALSE);
	OnSelchangeList();
}


I have highlighted the lines containing the errors.

Thanks,
Tony
In void CMagicFrame::UpdateFilterLists()
CMailFilter& mf = theApp.m_Filters.ElementAt(i);
This would be CMailFilter* mf = theApp.m_Filters.ElementAt(i);

In void CExcerpt::CheckByFilters() and void DFilters::GetData()
 
CMailFilter& mf = theApp.m_Filters.ElementAt(i);

This would be CMailFilter* mf = theApp.m_Filters.ElementAt(i);

On void DFilters::OnOK() and void DFilters::GetData() these are probably not needed.
1
2
3
theApp.m_Filters.Copy(m_Filters);

mf.m_sName becomes mf->m_sName


Is there a chance that you can upload the whole project somewhere?

If this doesn't work the last option would be to use std::vector<CMailFilter>
Thomas,

You should be able to get the whole project in a zip file from here:
https://drive.google.com/file/d/0B3Jw7h2K4Zvkdm9qX19WM0hXaDg/view?usp=sharing

Thanks,
Tony
I have downloaded it but it doesn't compile. openssl files are missing.

BTW: did you try it with pointers ?
Thomas,

Here is the link to the OpenSSL zip files:
https://drive.google.com/file/d/0B3Jw7h2K4ZvkN3hpX3hjTS1vTTg/view?usp=sharing

Wherever you unzip those files to, you will have to change the project "Configuration Properties\VC++ Directories\Include Directories" to point to it and the same for "Configuration Properties\VC++ Directories\Library Directories".

Yes, I did try the pointers method and I get another 13 errors. Should I try to resolve those also?

Thanks,
Tony
Thomas,

I reviewed some of the changes I made to eliminate errors when I first migrated this package from VS 6.0 to VS 2015. There were two for loops where I had to define an "i" int variable before the loops instead of in the loops. I saw where I changed the code incorrectly and changed it to the way it should be.

After that I applied your suggested changes from Jul 17, 2016 at 4:23am like this:

// m_Filters.SetSize(1);
// m_Filters.SetAt(0, filter);
m_Filters.Add(filter)

The program compiles and runs without error.

If I reverse your suggested changes, it still gives the error that I was getting after originally making the changes that you suggested.

Should I leave your suggested changes in? I don't see any problems with the program yet, but I have not tested a parts of it.

Thanks,
Tony

closed account (E0p9LyTq)
@Carneno,

Something in VS 2015 I just ran across that might help you migrate your project:

VS has the ability to create a new project using existing files. I used that feature to migrate a Win32 and MFC VS 6.0 projects to VS 2015.
Should I leave your suggested changes in? I don't see any problems with the program yet, but I have not tested a parts of it.


I wouldn't make changes as long as it works. Even small changes can lead to new problems.
Thomas,

I just want to make sure that what I am doing is the right thing.

After making numerous changes to fix compile errors, I was still getting the error that I posted to start this thread. You gave me this change to fix that error:
1
2
3
// m_Filters.SetSize(1);
 // m_Filters.SetAt(0, filter);
 m_Filters.Add(filter)


The program now works with those changes.

Should I leave those changes in?

Thanks,
Tony
The program now works with those changes.
Should I leave those changes in?


Yes I would leave it like it is
Pages: 123