Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Access violation from System::SysFreeMem()


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 8 - Last Post: Sep 29, 2016 3:11 AM Last Post By: Ville-Valtteri ...
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 18, 2016 11:08 PM
I am receiving access violations randomly in various parts of the software. Access violation happens in System::SysFreeMem() and is apparently related to deallocating internal buffer of an std string. This happens at least with Debug build.

I am using C++ Builder 10.1, developing on Windows 7, building for 32-bit Windows target, and running on Windows 7 with debugger attached. I have not been able to create a minimal working example.

Why does this happen, and how to fix this?

Examples:

Inserting to locally defined wstring: http://i.imgur.com/AmacGGJ.png

Destruction of temporary string: http://i.imgur.com/23AEAvT.png

Relevant library code:

System::SysFreeMem(): http://i.imgur.com/c6OaU7b.png

basic_string::_Tidy(): http://i.imgur.com/Vv5wzBV.png
Andrew Law

Posts: 74
Registered: 11/6/02
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 19, 2016 1:31 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
Ville-Valtteri Tiittanen wrote:
I am receiving access violations randomly in various parts of the software. Access violation happens in System::SysFreeMem() and is apparently related to deallocating internal buffer of an std string. This happens at least with Debug build.

You probably need to tell us if you are using the classic compiler, or the newer clang compiler.

If you don't need your code to be compiler-portable, have you tried using UnicodeString instead of wstring?

Inserting to locally defined wstring: http://i.imgur.com/AmacGGJ.png

You're using the += operator and several instances of the + operator all on one line. While this may be allowed, and normal in other circumstances, may I suggest breaking this one line into four lines, where each additional string portion is added one at a time?
e.g.
mystring += str1;
mystring += str2;
mystring += str3;
mystring += str4;


Either that, or try assembling the four strings on the right of your += operator into a single string first of all, and then append the entire string in one go.

Destruction of temporary string: http://i.imgur.com/23AEAvT.png

You are passing a temporary string into your constructor for Statement. Have you tried storing the result of .str() as a declared string variable? Doing two things on one line makes it hard to tell if it is the call to .str(), or the call to your Statement constructor, that is causing the problem.

I don't know why this is happening, but I do remember some funny issues with AnsiString (now UnicodeString), if appending text to VCL form captions.
For example,
Application->MainForm->Caption += strVersionNumber;


can behave differently from what one would naturally expect. Instead, writing
String strCaption =  Application->MainForm->Caption;
strCaption += strVersionNumber;
Application->MainForm->Caption = strCaption;


does what was expected. Somewhere there was a forum post or help topic explaining why.
{code}
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 19, 2016 4:32 AM   in response to: Andrew Law in response to: Andrew Law
Andrew Law wrote:
You probably need to tell us if you are using the classic compiler, or the newer clang compiler.

I am using the newer clang compiler.
Andrew Law

Posts: 74
Registered: 11/6/02
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 19, 2016 5:32 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
Ville-Valtteri Tiittanen wrote:
Andrew Law wrote:
You probably need to tell us if you are using the classic compiler, or the newer clang compiler.

I am using the newer clang compiler.
	std::wstring str1 = L"Hello, ";
	std::wstring str2 = L"world, ";
	str1 += (str2 + L"how " + L"are " + L"you? ");


works fine for me using the clang compiler in 10.1. So it's not the fundamental string concatenation that's the root fault here, I would suggest.

Please can you verify that my trivial example works for you, and then try to break down your line of compound operations into multiple lines of operation so that it is easier to debug one line at a time.

If my example above works for you, then you can modify one item at a time, testing each time, to see which of your items introduces the fault as you replace my dummy items with your actual items. I repeat my suggestion of storing the compound string that you are assembling as an actual variable, before calling the += operator, to see if that makes any difference. There is no benefit in having single lines of code do multiple steps in one go, if it doesn't actually work out in practice.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Access violation from System::SysFreeMem() [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 19, 2016 11:22 AM   in response to: Andrew Law in response to: Andrew Law
Andrew wrote:

I don't know why this is happening, but I do remember some funny
issues with AnsiString (now UnicodeString), if appending text to VCL
form captions.

For example,

 
Application->MainForm->Caption += strVersionNumber;
 


can behave differently from what one would naturally expect.

In the classic compiler, that syntax does not append the text to the property.
It reads the property into a temp and then appends to the temp, but the
temp is not assigned back to the property.

In the clang compiler, that syntax works as expected, the text is appended
to the property. This is documented behavior:

Differences Between Clang-enhanced C++ Compilers and Previous-Generation
C++ Compilers:
__property: Compound and Chained Assignment

http://docwiki.embarcadero.com/RADStudio/en/Differences_Between_Clang-enhanced_C%2B%2B_Compilers_and_Previous-Generation_C%2B%2B_Compilers#property:_Compound_and_Chained_Assignment

--
Remy Lebeau (TeamB)
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2016 3:10 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
This was caused by buffer overflow somewhere else in the software. The code had reserved memory for an std::string and wrote one byte past the end, which corrupted the memory.
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2016 3:11 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
This was caused by buffer overflow somewhere else in the software. The code had reserved memory for an std::string and wrote one byte past the end, which corrupted the memory.
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2016 3:11 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
This was caused by buffer overflow somewhere else in the software. The code had reserved memory for an std::string and wrote one byte past the end, which corrupted the memory.
Ville-Valtteri ...

Posts: 13
Registered: 6/18/15
Re: Access violation from System::SysFreeMem()  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2016 3:11 AM   in response to: Ville-Valtteri ... in response to: Ville-Valtteri ...
This was caused by buffer overflow somewhere else in the software. The code had reserved memory for an std::string and wrote one byte past the end, which corrupted the memory.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02