1

I have following code:

CAtlString str = currentFolder->whatsThis().toStdWString().c_str();

currentFolder is qt QTreeWidgetItem*. When I run program from VS2010 it works ok. But when I start program by double click in the same folder (x64/Debug or Release), I have exceptions:

Unhandled exception at 0x00007ffe2a07572a (ntdll.dll) in test.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.

on dealloc in basic_string destructor:

~basic_string()      // xstring
    {   // destroy the string
    _Tidy(true);
    }

// ...

void deallocate(pointer _Ptr, size_type) // in xmemory
    {   // deallocate object at _Ptr, ignore size
    ::operator delete(_Ptr);
    }

I`m trying to delete string directly, clean, erase - but every time I had the same result. Also I had tried to use different runtimes and deploy packages, without lucky. (searched with Dependency Walker). Why program works under VS2010? And how can I fix standalone running? thx!

Update This is the minimal code reproduced the problem: zalil.ru use x64 config and run program by double click for catch the exception.

Update2 if I keep wstring pointer alive (using temp variable), I have following error: enter image description here

6
  • Are whatsThis() and toStdWString() built in functions or are they yours? If they are your can you show there code? Commented Jun 28, 2016 at 12:44
  • He said currentFolder is object of QTreeWidgetItem class. So those mehods are qt built-in Commented Jun 28, 2016 at 12:49
  • its qt functions and they return a valid values. Commented Jun 28, 2016 at 12:59
  • @Meteoir3 Only when called on valid objects ((QTreeWidgetItem*)nullptr)->whatsThis() will return something invalid, as I said in my answer Commented Jun 28, 2016 at 13:47
  • in debugger I saw valid string. Also when I run program from VS2010 string is valid, because all UI will not work without valid value of that string) Commented Jun 28, 2016 at 14:05

3 Answers 3

1

When you debug your app in Visual Studio with your default project settings, working folder is set to folder, where your sources are. And when you run app standalone, working folder is folder where your exe file is.

Error says it fails to deallocate std::wstring, which is made from QString::toStdWstring() method. QString is returned by whatsThis() method. As std::wstring is invalid, QString is also invalid. If currentFolder pointer is a valid pointer, it couldn't return invalid QString. So currentFolder is an invalid pointer.

Considering your working dir changed, probably currentFolder wasn't initialized properly.

Sign up to request clarification or add additional context in comments.

7 Comments

Thx for answer, but currentFolder is not a folder in file system - is QTreeWidgetItem* in database, that readed successfully. Exception has been thrown by click on currentFolder item on the line above. currentFolder = model->itemFromIndex(index);. In debugger I can see that wstring constructs properly. And when I copy program to sources folder I have the same exceptions.
what code I need to show? Im using dll. This DLL loads database and throw dll API Im calling database reading. But it seems that API works without errors. All works ok, except for string deallocation in ntdll.dll. Also everything are ok if program starts from VS2010.
@Meteoir3 The code, where currentFolder is initialized, as I am 99% sure it is invalid
currentFolder = model->itemFromIndex(index); - its initialize code for currentFolder. In debugger its valid)
@Meteoir3 You get error only when debugger is not present. Do something like currentFolder = model->itemFromIndex(index); if (!currentFolder) //ShowMessageBox and run it, as you say, 'standalone'
|
1

When you use what .c_str() returned you are using dangling pointer, as std::wstring object gets destroyed, but pointer, which c_str() returned still lives. c_str() returns inner pointer, memory to which it points is destroyed in std::wstring's destructor.

Accessing a dangling pointer is UB, that's why you never know will memory it points to be still valid or not

Workaround: keep std::wstring until you doesn't need c_str() anymore:

auto temp = currentFolder->whatsThis().toStdWString();
CAtlString str = temp.c_str();

3 Comments

Thx, but I have already tried this trick with temp variable. In this case there are no errors in that strings, but when program "out of scope" (leave onFolderClicked() function in example), I still have Assert error in dbgheap.c -> _pFirstBlock == pHead - only when I run program by double click. Do you have the same assert error?
Assertion failed message added in question. The same error on string deallocation: ::operator delete(_Ptr);
It seems that .toStdWString() also crashed the program! I think problem is not in code, problem is CRT. When I start program in VS - its ok, but start by double-click is missed to use CRT for std strings. Do you know how can I test that guesswork?
0

I have found problem solution. As I can understood, on my computer I have mixed installed Qt libraries (3 different versions) and when std::string destructor called, I have UB because wrong runtime called. To solve my problem, I need to avoid using toStdWString/c_str in QString conversions //for example like this auto arr = currentFolder->whatsThis().toUtf8(); std::string temp_str(arr.data()); qDebug() << temp_str.c_str();

or recompile/clean reinstall all libraries that I have used. Its so strange configuration error) There is the page with decision: forum.qt.io

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.