getCarName
likely returns a temporary. After the assignment the temporary object is destroyed and the pointer item.pszText
points to invalid memory. You must ensure that the string object is valid during the call to ListView_InsertItem
.
std::string text(record.getCarName()); item.iSubItem = 0; item.pszText = const_cast<LPSTR>(text.c_str()); item.iItem = 0; ListView_InsertItem(CarsListView, &item);
The const_cast
is an artifact of the fact that the Windows API uses the same structure to set and retrieve information. When invoking ListView_InsertItem
the structure is immutable, however there is no way to reflect that in the language.
Related Posts:
- What does LPCWSTR stand for and how should it be handled with?
- Can I output a one channel image acquired from camera into a winAppi window?
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- Sleep for milliseconds
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- How to throw a C++ exception
- The static keyword and its various uses in C++
- What is the ‘override’ keyword in C++ used for? [duplicate]
- Unresolved external symbol in object files
- Mutex example / tutorial? [closed]
- “std::bad_alloc”: am I using too much memory?
- What does “dereferencing” a pointer mean?
- What is a smart pointer and when should I use one?
- check if a std::vector contains a certain object?
- Error: No viable overloaded =
- Random number between 1 to 10 using C++
- Expected initializer before namespace
- A proper way to create a matrix in c++
- No matching member function for call to ‘push_back’ error
- “X does not name a type” error in C++
- Difference between long double and double in C and C++ [duplicate]
- Why there is no pop_front method in C++ std::vector?
- How do I generate a random number between two variables that I have stored?
- How to print pthread_t
- make: g++: Command not found
- Deep copy vs Shallow Copy
- Pointer to incomplete class type is not allowed
- C++ error: “Array must be initialized with a brace enclosed initializer”
- Why am I getting string does not name a type Error?
- How to avoid the error: terminate called after throwing an instance of ‘std::logic_error’ what(): basic_string::_S_construct null not valid
- Error while overloading operator (must be a nonstatic member function)
- C++ Fatal Error LNK1120: 1 unresolved externals
- Vector of structs initialization
- C++ deprecated conversion from string constant to ‘char*’
- Converting a vector
to string - Checking cin input stream produces an integer
- Where do “pure virtual function call” crashes come from?
- How to convert vector to array
- “non-standard syntax; use ‘&’ to create a pointer to member” error in Visual Studio 2015
- libpng warning: iCCP: known incorrect sRGB profile
- C++: Expression must have a constant value when declaring array inside function
- What is the best open XML parser for C++?
- *** No rule to make target ‘class.cpp’, needed by `build/….x86/class.o` Stop. error in Ubuntu
- What’s the difference between opening a file with ios::binary or ios::out or both?
- Hash function for a string
- CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
- How to run valgrind with basic c example?
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- cc1plus: error: unrecognized command line option “-std=c++11” with g++
- cannot declare variable ‘’ to be of abstract type ‘’
- Difference between ‘struct’ and ‘typedef struct’ in C++?
- Debug vs Release in CMake
- C++ Matrix Class
- access violation reading location c++
- error: called object type ‘int’ is not a function or function pointer
- Hash function for a string
- Is there a replacement for unistd.h for Windows (Visual C)?
- How to call on a function found on another file?
- How to project a point onto a plane in 3D?
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- generate random double numbers in c++
- Cygwin Make bash command not found
- C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015
- Why use pointers?
- Why I cannot cout a string?
- How do I return a char array from a function?
- c++ reading csv file
- Don’t understand static boolean behavior
- How to write std::string to file?
- Member declaration not found
- What is the difference between a .cpp file and a .h file?
- Stable Cotangent
- How to change string into QString?
- Including .cpp files
- Class template inheritance C++
- C++ error: undefined reference to ‘clock_gettime’ and ‘clock_settime’
- Converting String to Cstring in C++
- creating dynamic array of string c++
- How well is Unicode supported in C++11?
- Initializing default values in a struct
- Warning: comparison of distinct pointer types
- Two decimal places using printf( )
- error C2244 unable to match function definition to an existing declaration
- Initializing pointers in C++
- Static vs dynamic type checking in C++
- “No rule to make target ‘install'”… But Makefile exists
- How to determine the version of the C++ standard used by the compiler?
- Make / gcc cryptic error 2: how to have more information?
- REGSVR32: the module “xxxxx.dll” failed to load … dependent assembly could not be found
- Error: expression cannot be used as a function?
- C++ JSON Serialization
- stack around the variable…was corrupted
- Creating a list to hold objects in C++
- What does “warning: not all control paths return a value” mean? (C++)
- Array of Linked Lists C++
- Is it still safe to delete nullptr in c++0x?
- Declaration is incompatible with type
- One or more multiply defined symbols found
- I’m getting the error “stoi is not a member of std” in myprogramminglab [duplicate]