www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - String concatenation segmentation error

reply tcak <tcak gmail.com> writes:
string fileContent = "";

...

writeln(ri, ": debug 1");
foreach(i; 0..dim)
{
	if( i > 0 ){ fileContent ~= "\t"; }

	writeln(ri, ": debug 1.1: ", ri*dim + i, ": ", positions[ ri*dim 
+ i ]);

	fileContent ~= to!string(positions[ ri*dim + i ]);

	writeln(ri, ": debug 1.2: ", ri*dim + i, ": ", positions[ ri*dim 
+ i ]);
}

---------

On line "fileContent ~= ...", I get a segmentation fault.

"positions" array is defined as auto positions = new float[ 100 
]; So, I am 100% sure, it is not out of range. "ri*dim + 1" is 
not a big number at all.

...
4: debug 1.1: 9: 0.271075
4: debug 1.2: 9: 0.271075
4: debug 2
4: debug 2.1: 4
4: debug 3
4: debug 4
5: debug 1
5: debug 1.1: 10: 0.884978
5: debug 1.2: 10: 0.884978
5: debug 1.1: 11: 0.813104
Segmentation fault
...

I have compiled the code with "-g" flag and ran it with GNU 
debugger. It gives following:

Thread 1 "dataspace" received signal SIGSEGV, Segmentation fault.
0x00005555556ca286 in 
_D2gc4impl12conservativeQw3Gcx10smallAllocMFNbmKmkxC8TypeInfoZPv 
()


So, there is a problem about small allocation. I remember I had 
this problem before in another project.

I have enough free ram. htop shows 3.96 GiB of 8 GiB is used only 
and swap is not in use.

DMD64 D Compiler v2.094.0

Is this error related to me? Is it a programmer error? Is it a 
bug? Am I doing something wrong? This is a compiler related 
operation (string concatenation), and I assume/expect that it 
would work without a problem.
Apr 22 2021
next sibling parent tcak <tcak gmail.com> writes:
In other parts of the code, concatenation operations are all 
failing with same error. I need guidance to get out of this 
situation. My assumption was that as long as there is empty heap 
memory, concatenation operation would succeed always. But, it 
doesn't seem like so.
Apr 22 2021
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
Are there any other threads in your program?
Apr 22 2021
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 22 April 2021 at 21:15:48 UTC, tcak wrote:
 "positions" array is defined as auto positions = new float[ 100 
 ]; So, I am 100% sure, it is not out of range. "ri*dim + 1" is 
 not a big number at all.
Oh and *where* is that positions variable defined?
Apr 22 2021
parent reply tcak <tcak gmail.com> writes:
On Friday, 23 April 2021 at 00:30:02 UTC, Adam D. Ruppe wrote:
 On Thursday, 22 April 2021 at 21:15:48 UTC, tcak wrote:
 "positions" array is defined as auto positions = new float[ 
 100 ]; So, I am 100% sure, it is not out of range. "ri*dim + 
 1" is not a big number at all.
Oh and *where* is that positions variable defined?
I am doing OpenCL programming. CPU side is single threaded. As far as I see, it is not related to that array or indices at all. After running for a short time, if a piece of code does any string/char or byte array concatenation at all, all of them cause segmentation fault with error: _D2gc4impl12conservativeQw3Gcx10smallAllocMFNbmKmkxC8TypeInfoZPv () When I comment out those piece of codes, there is no error. If there is no known situation that would cause this, I will need to update codes to C-style pre-allocate buffer and copy inside it instead of concatenating data.
Apr 22 2021
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 23 April 2021 at 00:44:58 UTC, tcak wrote:
 As far as I see, it is not related to that array or indices at 
 all.
The question of where is to see if it was CTFE allocated or runtime allocated. I don't think it should make a difference here but idk.
 If there is no known situation that would cause this
druntime not being initialized is the only situation I know of that causes this directly. Otherwise memory corruption or race condition of some sort, hence why i was wondering about threads. But I don't think it is uninitialized druntime unless that new happened at ctfe or something.
Apr 22 2021
prev sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 22 April 2021 at 21:15:48 UTC, tcak wrote:
 string fileContent = "";

 ...

 [...]
Do you have a minimal reproducible test case? 🤔
Apr 23 2021