Skip to content

Commit b7f7d47

Browse files
committed
fix c++20 crashes
1 parent bdf3ac4 commit b7f7d47

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/tbprobe.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,25 @@ bool tb_init(const char *path)
905905
TB_MaxCardinality = TB_MaxCardinalityDTM = 0;
906906

907907
if (!pieceEntry) {
908+
#if defined(__cplusplus) && (__cplusplus >= 202002L)
909+
/* Fix crash with -std=c++20 by being consistent with BaseEntry initialization:
910+
911+
#if __cplusplus >= 202002L
912+
atomic<bool> ready[3]{false, false, false};
913+
#else
914+
...
915+
916+
C++ initialization does not work with malloc.
917+
Also: with new, if the allocation fails we get std::bad_alloc exception,
918+
which is better library behavior than just calling exit.
919+
*/
920+
921+
pieceEntry = new PieceEntry[TB_MAX_PIECE]();
922+
pawnEntry = new PawnEntry[TB_MAX_PAWN]();
923+
#else
908924
pieceEntry = (struct PieceEntry*)malloc(TB_MAX_PIECE * sizeof(*pieceEntry));
909925
pawnEntry = (struct PawnEntry*)malloc(TB_MAX_PAWN * sizeof(*pawnEntry));
926+
#endif
910927
if (!pieceEntry || !pawnEntry) {
911928
fprintf(stderr, "Out of memory.\n");
912929
exit(EXIT_FAILURE);
@@ -1027,9 +1044,14 @@ bool tb_init(const char *path)
10271044
void tb_free(void)
10281045
{
10291046
tb_init("");
1047+
#if defined __cplusplus && __cplusplus >= 202002L
1048+
delete[] pieceEntry;
1049+
delete[] pawnEntry;
1050+
#else
10301051
free(pieceEntry);
1031-
pieceEntry = NULL;
10321052
free(pawnEntry);
1053+
#endif
1054+
pieceEntry = NULL;
10331055
pawnEntry = NULL;
10341056
}
10351057

0 commit comments

Comments
 (0)