From: mzdude on
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;
}
From: Stephan T. Lavavej [MSFT] on
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

"mzdude" <jsanga(a)cox.net> wrote in message
news:23f3831d-a65c-4464-97ae-071432596956(a)a9g2000vbb.googlegroups.com...
> 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;
> }

From: David Lowndes on
>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