Discussion:
Legacy Code update problems.
(too old to reply)
mzdude
2010-04-05 19:49:10 UTC
Permalink
We are currently bringing some old VC 6 code forward to VC 2008.
We ran into a problem with the following code snippet. Any ideas
why a CArray of vectors won't work? The easy and obvious solution
is to just make a vector of vectors.

The code appears to function correctly in DEBUG, but will
crash in RELEASE mode.

#include "stdafx.h"
#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CWinApp theApp;

using namespace std;
typedef std::vector<int> TEST_INT_VECTOR;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL,
::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
//This piece of code works fine in debug mode but throws an
// exception in release mode (with default settings).
CArray<TEST_INT_VECTOR, TEST_INT_VECTOR> testArray;
TEST_INT_VECTOR vObj1(1);
testArray.Add(vObj1);
TEST_INT_VECTOR vObj2(1);
testArray.Add(vObj2);
testArray[0].push_back(5);
}

return nRetCode;
}
Stephan T. Lavavej [MSFT]
2010-04-05 21:10:14 UTC
Permalink
CArray memcpys its elements and is therefore incompatible with non-PODs (POD
= Plain Old Data), such as STL containers.

You should use a vector of vectors. It's much more efficient than you might
expect (due to VC9's Swaptimization, and VC10's rvalue references).

Stephan T. Lavavej
Visual C++ Libraries Developer
Post by mzdude
We are currently bringing some old VC 6 code forward to VC 2008.
We ran into a problem with the following code snippet. Any ideas
why a CArray of vectors won't work? The easy and obvious solution
is to just make a vector of vectors.
The code appears to function correctly in DEBUG, but will
crash in RELEASE mode.
#include "stdafx.h"
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CWinApp theApp;
using namespace std;
typedef std::vector<int> TEST_INT_VECTOR;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL,
::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
//This piece of code works fine in debug mode but throws an
// exception in release mode (with default settings).
CArray<TEST_INT_VECTOR, TEST_INT_VECTOR> testArray;
TEST_INT_VECTOR vObj1(1);
testArray.Add(vObj1);
TEST_INT_VECTOR vObj2(1);
testArray.Add(vObj2);
testArray[0].push_back(5);
}
return nRetCode;
}
David Lowndes
2010-04-05 21:17:05 UTC
Permalink
Post by mzdude
We are currently bringing some old VC 6 code forward to VC 2008.
We ran into a problem with the following code snippet. Any ideas
why a CArray of vectors won't work? The easy and obvious solution
is to just make a vector of vectors.
The code appears to function correctly in DEBUG, but will
crash in RELEASE mode.
I can repro this with VS2008 (SP1), and I can see no reason for it not
to work.

With VS2010 RC there's no apparent problem, so I'd guess it's
something that's been fixed.

Dave

Loading...