Search code examples
visual-c++vectoriterationcorruptlpcwstr

Corrupted vector entries with LPCWSTR vector


I ahve the following piece of code. I get a correctly filled vector. But I am unable to print or use the vector contents which are file names from a directory. As soon as I do enter the first iteration. Everything gets lost. What am I doing wrong?

wprintf - This works OK

wcout-- here is where everything ends up corrupted

#include <windows.h>
#include <sstream>
#include <string>
#include <vector>
#include<iostream>
void GetAllFiles(vector<LPCWSTR>&, wstring);
using namespace std;
void main (void)
{
    vector<LPCWSTR> files(0);
    wstring path = L"Datasets\\Persons\\";
    wstring ext = L"*.*";
    wstring fullPath = path+ext;
    GetAllFiles(files,fullPath);    
    for (unsigned i=0; i<files.size() ; i++)
    {
        try
        {
            wcout<<"::\n"<<files[i];
        }
        catch(exception &ex)
        {
            cout<<"Error:"<<ex.what();
        }
    }

}

void GetAllFiles(vector<LPCWSTR>& fileNames,wstring dir)
{

    WIN32_FIND_DATA search_data;
    memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
    HANDLE handle = FindFirstFile(dir.c_str(),&search_data);
    while(handle != INVALID_HANDLE_VALUE)
    {
        wprintf(L"Found file: %s\r\n", search_data.cFileName);
        fileNames.push_back(search_data.cFileName);
        if(FindNextFile(handle, &search_data) == FALSE)
            break;
    }   
}

I have attached a screen shots of the output.

Correct when read off disk in GetAllFiles(...)

Corrupted as soon as the loops first iteration is done


Solution

  • search_data.cFileName is a pointer to memory controlled by the FindFirstFile/FindNextFile iterator interface; you cannot store this pointer value as the pointed-to memory could change from iteration to iteration (or even be freed after the iteration completes).

    Instead, you must make a copy of the string to put in your vector, e.g. using wcsdup. Even better, define your vector as a vector<wstring>, so that push_back(search_data.cFileName); creates a wstring with the contents of search_data.cFileName.