Repository: pocoproject/poco

Issue #292: Segmentation Fault when using priority on an Logger
Created: 2013-09-20T21:16:08Z
I play with the logger library. Here is the code
// Create the logger Channels
Poco::AutoPtr<ColorConsoleChannel> pcConsoleBase(new ColorConsoleChannel); 
pcConsoleBase->setProperty("noticeColor","lightGreen");
Poco::AutoPtr<PatternFormatter> pcConsoleFmt(new PatternFormatter);
pcConsoleFmt->setProperty("pattern", "(%H:%M:%S) %s[%p]: %t");
Poco::AutoPtr<FormattingChannel> pcConsole(new FormattingChannel(pcConsoleFmt,pcConsoleBase));

// Create the file logger
Poco::AutoPtr<FileChannel> pcFileBase(new FileChannel);
pcFileBase->setProperty("path", LOGGER_FILE);
pcFileBase->setProperty("rotation", "2 K");
pcFileBase->setProperty("archive", "timestamp");
Poco::AutoPtr<PatternFormatter> pcFileFmt(new PatternFormatter);
pcFileFmt->setProperty("pattern","(%H:%M:%S) %s[%q]: %t");
Poco::AutoPtr<FormattingChannel> pcFile(new FormattingChannel(pcFileFmt,pcFileBase)); 

// Create the Splitter File (Compines Console and File)
Poco::AutoPtr<SplitterChannel> pcSplitter(new SplitterChannel);
pcSplitter->addChannel(pcConsole);
pcSplitter->addChannel(pcFile);

// Create an asynchronus channel. Set the Root channel to async mode
Poco::AutoPtr<AsyncChannel>    pcAsync(new AsyncChannel(pcSplitter));
pcAsync->setProperty("priority","lowest");
Logger::root().setChannel(pcAsync);

When i add %p on the formatter i get a seg fault
(gdb) run
Starting program: /home/atlantis/Projects/atlantis/build/dist/bin/Log1 
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff6857700 (LWP 31446)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff6857700 (LWP 31446)]
__memcpy_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:118
118 ../sysdeps/x86_64/multiarch/memcpy-ssse3.S: No such file or directory.
(gdb) backtrace
#0  __memcpy_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:118
#1  0x00007ffff7604462 in std::string::append(std::string const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff79124ab in Poco::PatternFormatter::format(Poco::Message const&, std::string&) () from /usr/local/lib/libPocoFoundation.so.16
#3  0x00007ffff78f0243 in Poco::FormattingChannel::log(Poco::Message const&) () from /usr/local/lib/libPocoFoundation.so.16
#4  0x00007ffff791c01c in Poco::SplitterChannel::log(Poco::Message const&) () from /usr/local/lib/libPocoFoundation.so.16
#5  0x00007ffff78cd408 in Poco::AsyncChannel::run() () from /usr/local/lib/libPocoFoundation.so.16
#6  0x00007ffff7928f12 in Poco::ThreadImpl::runnableEntry(void*) () from /usr/local/lib/libPocoFoundation.so.16
#7  0x00007ffff7bc4f8e in start_thread (arg=0x7ffff6857700) at pthread_create.c:311
#8  0x00007ffff7062e1d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:113

If i remove the %p or don't use the AsyncChannel the program works.

Duplicate issue #1009: Logger API should take AutoPtr instead of plain pointer
Created: 2015-11-02T13:58:01Z
In all the examples for Logging you use AutoPtr to store the channels. This makes perfect sense. But the Logger API only takes plain pointers and those are not counted with AutoPtr.
So the examples only work if you store the Channels on the stack where you do not remove them (like main function for example), if you store them differently or randomly.
I was getting a segfault because of this in log(Message&).
Consider the following code:

void configurePocoLogger()
{
AutoPtr pNULL(new NullChannel);
AutoPtr pFile(new SimpleFileChannel);
pFile->setProperty("path", "/tmp/beforeValid.log"); //"/tmp/beforeValid.log"
pFile->setProperty("rotation", "never");
Poco::Logger::root().setChannel(pFile);
Poco::Logger::root().setLevel(Poco::Message::PRIO_TRACE);
}
int main(int argc, char** argv)
{
configurePocoLogger();
Logger& logger = Logger::root();
logger.warning("Testing string function");
return 0;
}

After configurePocoLogger() is finished AutoPtr will remove the object because a plain ptr is not counted.
Actually the code above somehow works with gcc - the code actually failing was like the following what is obviously wrong but should be the same in my eyes:

void configurePocoLogger()
{
AutoPtr pNULL(new NullChannel);
Channel* pCh = pNULL;
if (true)
{
AutoPtr pFile(new SimpleFileChannel);
pFile->setProperty("path", "/tmp/beforeValid.log"); //"/tmp/beforeValid.log"
pFile->setProperty("rotation", "never");
pCh = pFile;
}
Poco::Logger::root().setChannel(pCh);
Poco::Logger::root().setLevel(Poco::Message::PRIO_TRACE);
}
int main(int argc, char** argv)
{
configurePocoLogger();
Logger& logger = Logger::root();
logger.warning("Testing string function");
return 0;
}

I think for the first code the compiler somehow detects that the stack variable is still used so I am not 100% sure but I think it at least could go wrong.

Closing comment (2015-11-02T13:59:31Z): may be related to #292