'signal SIGSEGV, Segmentation fault. __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65
I have converted my old code:
#define MAX_LOG_MSG 2048
in the *.h file
typedef void (*LogMessageFunction)(char *);
in the *.cpp file
static LogMessageFunction m_MessageFunctions[LastLogCount] = {NULL};
void DebugMsg(const char* fmt, ...)
{
if (!m_MessageFunctions[DebugLevel]) return;
char msgBuffer[MAX_LOG_MSG];
va_list argList;
va_start(argList, fmt);
vsnprintf(msgBuffer, MAX_LOG_MSG, fmt, argList);
va_end(argList);
m_MessageFunctions[DebugLevel](msgBuffer);
}
into a dynamic allocated char*
void DebugMsg(const char* fmt, ...)
{
if (!m_MessageFunctions[DebugLevel]) return;
va_list argList;
va_start(argList, fmt);
size_t size = vsnprintf(NULL, 0, fmt, argList) + 1;
va_end(argList);
char* msgBuffer = new char[size];
va_start(argList, fmt);
vsnprintf(msgBuffer, size, fmt, argList);
va_end(argList);
m_MessageFunctions[DebugLevel](msgBuffer);
delete[] msgBuffer;
}
on windows everything works great now.
Thread 1 "ATETests" received signal SIGSEGV, Segmentation fault. __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65 65 ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
as far as I can see this has something to do with the size of the buffer or the string? Can you help?
Solution 1:[1]
I'd make a variadic template out of it instead and use a std::unique_ptr<char[]> for the memory allocation:
#include <memory>
template<class... Args>
void DebugMsg(const char* fmt, Args&&... args)
{
if (!m_MessageFunctions[DebugLevel]) return;
int size = std::snprintf(nullptr, 0, fmt, args...) + 1;
if (size < 1) return; // check for error
auto msgBuffer = std::make_unique<char[]>(size);
size = std::snprintf(msgBuffer.get(), size, fmt, args...);
if (size < 0) return; // probably unnecessary
m_MessageFunctions[DebugLevel](msgBuffer.get());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
