Could you explain this issue of removing C ++?

I have the following code:

std::string F() { WideString ws = GetMyWideString(); std::string ret; StringUtils::ConvertWideStringToUTF8(ws, ret); return ret; } 

WideString is a third-party class, therefore StringUtils. This is a black box for me. The second parameter is passed by reference.

When I exit through the debugger, the return ret line gives an unpleasant pop-up window (Visual C ++), which says that the heap may be damaged. On closer inspection, a copy of the string that is returned is OK, but deleting ret fails. ret contains the correct value before returning.

What can the conversion function do to trigger this? Any ideas for a fix?

Update:

  • The project itself is a dll
  • StringUtils is a lib
  • Project compiled against multi-threaded CRT (not debugging, not dll)
  • The program seems to work fine when run outside of Visual Studio
+2
source share
4 answers
  • If StringUtils was compiled separately (for example, with a different version of the compiler), you may have a conflict in the layout of the object.
  • If StringUtils is in the DLL, you must make sure that both it and the main program are compiled to use the standard library in the DLL. Otherwise, each module (executable and DLL) will have its own heap. When StringUtils tries to play with data in a string allocated from another heap, bad things happen.
+4
source

The designer of StringUtils has developed a very bad API. None of the standard template library types should be used in the public API. std::string out of order. Therefore, if the compiler and libraries you are using are not the same compiler and libraries that are used by the StringUtils developer, the types can and are likely to be different. In fact, the developer of StringUtils was unable to separate the interface from the implementation .

Illustration of a problem. Suppose you are using MSVC 9.0 SP1 and I am using MSVC 8.0. In my compiler, an implementation of std :: string might look like this:

 class string { // : : stuff private: int someInt_; char* someBuf_; }; 

... but on your compiler it might look different:

 class string { // : : stuff private: void* impl_; }; 

If I write a library function:

 void DoSomethingWithAString(std::string& str); 

... and you name it, sizeof(string) in your code will be different from sizeof(string) in my code. Types do NOT match.

You really only have 2 solutions to your problem:

1) [preferred] Get an implementation of StringUtils to fix its broken code.

2) Replace the library used by your compiler with the library used by the StringUtil developer. Perhaps you can do this using the same compiler at the same fix level as the developer used, assuming that he did not replace the standard library implementation.

EDIT: 3) The third option is to stop using StringUtils. Honestly, this is probably what I would do.

+2
source

From the little code you are showing, I assume that StringUtils::ConvertWideStringToUTF8() takes std::string& as the second parameter. Given this, I do not see how your code can cause heaps.

Please note, however, that linking C ++ libraries generally only works when all the codes have been compiled using the same compiler and with the same compiler settings.

+1
source

Using StringUtils and WideString means you are using C ++ Builder. Are you trying to mix the C ++ Builder module and the Visual C ++ module? If so, then you will definitely see the problems that you described.

You cannot pass Visual C ++ std::string to a C ++ Builder function, because C ++ Builder code will assume that the parameter uses the C ++ Builder std::string definition. Classes can have different fields, and the fields that they have can be in a different order.

Even if classes have the same definitions, modules will still use different memory managers. The called function will allocate memory for the new contents of the line using the memory manager, and the caller will use its own memory manager to try later to free the contents of the line.

0
source

Source: https://habr.com/ru/post/1303039/


All Articles