log4cxx is throwing exception on ~Logger - log4cxx

I just started with log4cxx by doing a small app to get familiar with it.
I compiled with Visual Studio 2005, no warnings or errors.
Basically looks like :
#includes<...>
...
...
LoggerPtr logger(Logger::getLogger("MyApp"));
void main(...)
{
//some logs here
}
It works as expected until I close the app when I get an exception while trying to destroy the global Logger object.
Here is the trace:
log4cxx.dll!apr_pool_cleanup_kill(apr_pool_t * p=0xdddddddd, const void * data=0x01cf6158, int (void *)* cleanup_fn=0x10174250) Line 1981 + 0x3 bytes
log4cxx.dll!apr_pool_cleanup_run(apr_pool_t * p=0xdddddddd, void * data=0x01cf6158, int (void *)* cleanup_fn=0x10174250) Line 2025
log4cxx.dll!apr_thread_mutex_destroy(apr_thread_mutex_t * mutex=0x01cf6158) Line 133
log4cxx.dll!log4cxx::helpers::Mutex::~Mutex() Line 57
log4cxx.dll!log4cxx::Logger::~Logger() Line 55 + 0xb bytes
log4cxx.dll!log4cxx::Logger::`vbase destructor'() + 0x19 bytes
log4cxx.dll!log4cxx::Logger::`vector deleting destructor'() + 0x5a bytes
log4cxx.dll!log4cxx::helpers::ObjectImpl::releaseRef() Line 46 + 0x39 bytes
log4cxx.dll!log4cxx::Logger::releaseRef() Line 63
log4cxx.dll!log4cxx::helpers::ObjectPtrT<log4cxx::Logger>::~ObjectPtrT<log4cxx::Logger>() Line 100 + 0x33 bytes
NodeBHeartBeat.exe!`dynamic atexit destructor for 'logger''() + 0x2b bytes
msvcr80d.dll!doexit(int code=0x00000000, int quick=0x00000000, int retcaller=0x00000001) Line 553
msvcr80d.dll!_cexit() Line 413 + 0xb bytes
msvcr80d.dll!__CRTDLL_INIT(void * hDllHandle=0x6c710000, unsigned long dwReason=0x00000000, void * lpreserved=0x00000001) Line 389
msvcr80d.dll!_CRTDLL_INIT(void * hDllHandle=0x6c710000, unsigned long dwReason=0x00000000, void * lpreserved=0x00000001) Line 214 + 0x11 bytes
ntdll.dll!774b9960()
Anybody has any idea why is this happening ?
Thanks

I managed to get around the problem by simply assigning "0" to the logger pointer.
The magic trick is located inside log4cxx source code in the file src/main/include/log4cxx/helpers/objectptr.h
That is, releaseRef() giving indirect control over object destruction order.
...
...
ObjectPtrT& operator=(const int& null) //throw(IllegalArgumentException)
{
//
// throws IllegalArgumentException if null != 0
//
ObjectPtrBase::checkNull(null);
T* oldPtr = exchange(0);
if (oldPtr != 0) {
oldPtr->releaseRef();
}
return *this;
}
...
...
So, for your code, simply add a line at the end :
#includes<...>
...
...
LoggerPtr logger(Logger::getLogger("MyApp"));
void main(...)
{
//some logs here
logger = 0; // RELEASE REFERENCE TO POINTER
// Make sure no other reference to logger exist
// ie: a thread that used a copy constructor on the logger object.
}

Putting your LoggerPtr inside of a class can resolve this issue, like:
class A
{
public:
A()
{
string appPath = getenv("APPDIR");
appPath = appPath + "/" + LOG4CXX_CONFIG_FILE_NAME;
// Load configuration file
DOMConfigurator::configure(appPath.c_str());
m_serviceLogger = Logger::getLogger("tuxedoService");
}
void log()
{
LOG4CXX_ERROR(m_serviceLogger, "This is a Test log");
}
private:
LoggerPtr m_serviceLogger;
};
A a;
int main()
{
a.log();
return 0;
}
Please note, if you replace constructor with a member function, like init(), and call init() in main before the log function, it will result segment fault as you described again.
This is a deinitialization sequence issue.

I'll chime in here with a similar case we had. We were referencing a logger that was not defined in our log4cxx configuration file. In some version of the library this was fine, but in others it caused a stack trace as follows:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x00007ffff72b3892 in std::_Rb_tree<std::string, std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> >, std::_Select1st<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > >, std::less<std::string>, std::allocator<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > > >::_M_erase(std::_Rb_tree_node<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > >*) ()
from /usr/lib/liblog4cxx.so.10
#2 0x00007ffff72b387b in std::_Rb_tree<std::string, std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> >, std::_Select1st<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > >, std::less<std::string>, std::allocator<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > > >::_M_erase(std::_Rb_tree_node<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > >*) ()
from /usr/lib/liblog4cxx.so.10
#3 0x00007ffff72b387b in std::_Rb_tree<std::string, std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> >, std::_Select1st<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > >, std::less<std::string>, std::allocator<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > > >::_M_erase(std::_Rb_tree_node<std::pair<std::string const, log4cxx::helpers::ObjectPtrT<log4cxx::Logger> > >*) ()
from /usr/lib/liblog4cxx.so.10
#4 0x00007ffff72b1a2a in log4cxx::Hierarchy::~Hierarchy() () from /usr/lib/liblog4cxx.so.10
#5 0x00007ffff72b1c49 in log4cxx::Hierarchy::~Hierarchy() () from /usr/lib/liblog4cxx.so.10
#6 0x00007ffff72c982a in log4cxx::spi::DefaultRepositorySelector::~DefaultRepositorySelector() ()
from /usr/lib/liblog4cxx.so.10
#7 0x00007ffff72c95bc in log4cxx::helpers::ObjectPtrT<log4cxx::spi::RepositorySelector>::~ObjectPtrT() ()
from /usr/lib/liblog4cxx.so.10
#8 0x00007ffff52e8e25 in __cxa_finalize () from /lib/x86_64-linux-gnu/libc.so.6
#9 0x00007ffff727c2b6 in ?? () from /usr/lib/liblog4cxx.so.10
#10 0x000000000000005b in ?? ()
So we simply had to remove the reference and all was good. It was a very innocuous looking statement:
LoggerPtr logger(Logger::getLogger("someloggerthatdoesnotexist"));

Related

Porting 32-bit to 64-bit C program "Exception Thrown: Read Access Violation 0xC0000005"

I am using VS2017 and have the following lines of C code (sorry can't post all contents of function):
char *dptr; /* Global variables */
static u_int16_t vers;
...
static int32_t
read_row(database_t *dbase, table_t *table, int32_t col_flag, int32_t flag) {
row_t *rowptr;
char marker;
void *new_row;
table_t *t_new;
size_t row_length;
size_t row_ = 1;
marker = *dptr++;
if (vers < VERS && marker != ROW) {
return -1;
}
/* Create a new row */
t_new = table;
row_length = t_new->row_length;
new_row = calloc(row_, row_length);
rowptr = new_row;
if (rowptr == NULL)
return -1;
...
The last line is causing "Exception thrown at 0x00000000778745A0 (ntdll.dll) in ****.exe: 0xC0000005: Access violation reading location 0x00000000787745A0" error.
During debug, when I hover my pointer on calloc, I can see its value is "0x000000013f63dc94". But somehow, the exception is thrown and this value is never assigned to new_row (its value remains "0xcccccccccccccccc").
I have noticed that this exception is thrown when calloc has a value >"0x00000001********". It seems to work fine if 8 MSB of the address are 0.
The definition of calloc() is from corecrt.malloc.h:
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count * _Size)
_ACRTIMP _CRT_JIT_INTRINSIC _CRTALLOCATOR _CRTRESTRICT
void* __cdecl calloc(
_In_ _CRT_GUARDOVERFLOW size_t _Count,
_In_ _CRT_GUARDOVERFLOW size_t _Size
);
Any ideas about what could be causing this exception and how to fix it would be appreciated. Btw, I am using Win 7 Pro with 8GB RAM.

Using valgrind - "Invalid read of size 1" for strlen

I'm trying to write code that sets the name of a Student object to a new name, but I'm coming across memory leak errors when creating a character array. I assume it has to do with /0 at the end of the array and isn't terminating properly, but I don't know how to properly fix this. Thanks for the help.
#include "student.h"
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
Student::Student(const char * const name, int perm) {
this->setName(name);
this->setPerm(perm);
}
int Student::getPerm() const {
return this->perm;
}
const char * const Student::getName() const {
return this->name;
}
void Student::setPerm(const int perm) {
this->perm = perm;
}
void Student::setName(const char * const newName) {
this->name = new char[strlen(newName)+1];
// this->name[srtlen(newName)+1] = '/0'; <---- My suggested fix, but doesn't work
strcpy(this->name,newName);
}
Student::Student(const Student &orig) {
this->setName(orig.getName());
this->setPerm(orig.getPerm());
}
Student::~Student() {
delete this->name;
this->perm = 0;
}
This is the valgrind error:
==13814== Invalid read of size 1
==13814== at 0x4C2BA12: strlen (vg_replace_strmem.c:454)
==13814== by 0x4F56FD6: UnknownInlinedFun (char_traits.h:267)
==13814== by 0x4F56FD6: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (basic_string.h:456)
==13814== by 0x401ED8: Student::toString[abi:cxx11]() const (student.cpp:64)
==13814== by 0x401A46: main (testStudent00.cpp:14)
==13814== Address 0x5302e8 is not stack'd, malloc'd or (recently) free'd
==13814==
Your assumption that you needed to add the 0 terminator is wrong, strcpy() will do that for you. Your attempt of doing so adds the 0 terminator one byte past the space you allocated (remember, array indexes start at zero), and the syntax is also wrong, you would need to do:
this->name[strlen(newName)] = '\0';
However, to fix your memory leak You need to delete the previous string, like
void Student::setName(const char * const newName)
{
delete [] this->name;
this->name = new char[strlen(newName)+1];
strcpy(this->name,newName);
}
Student::Student(const Student &orig) :
name(0) {
this->setName(orig.getName());
this->setPerm(orig.getPerm());
}
Student::~Student() {
delete [] this->name;
this->perm = 0;
}
Now, for this to work, you also need to fix your constructor and copy constructor to initialize the name member, so it isn't an uninitialized pointer for the first call to the setName() function, and you need to add an assignment operator too, so you can properly handle assignments.
Student::Student(const char * const name, int perm) :
name(0)
{
this->setName(name);
this->setPerm(perm);
}
Student &operator=(const Student &orig) {
this->setName(orig.getName());
this->setPerm(orig.getPerm());
}
Also, consider using std::string instead of your current low level way of handling strings, that way you don't need to even implement a copy constructor, assignment operator and destructor for this class, nor deal with correctly managing memory.

pcl_visualizer.h - fatal error LNK1120: 1 unresolved externals

error LNK2001: unresolved external symbol "public: virtual void __cdecl pcl::visualization::PCLVisualizer::FPSCallback::Execute(class vtkObject *,unsigned long,void *)" (?Execute#FPSCallback#PCLVisualizer#visualization#pcl##UEAAXPEAVvtkObject##KPEAX#Z)
1>C:\Users\hatea\Documents\Visual Studio 2015\Projects\PCLTester\x64\Debug\PCLTester.dll : fatal error LNK1120: 1 unresolved externals
I have thoroughly exhausted all avenues dealing with this issue. I checked here, and I found a similar question/answer series: error LNK2001 when including "pcl_visualizer.h"
The problem is that I do not want to comment out the FPSCallback method. I need it for the VTK visualizer I am using. I have determined that I only receive the unresolved external error when I reference the .h file in a managed C++/CLI library.
#pragma once
#pragma unmanaged
#include <pcl/visualization/pcl_visualizer.h>
#pragma managed
using namespace System;
namespace PCLTesterCLI
{
public ref class PCLTesterCLI
{
public:
PCLTesterCLI();
virtual ~PCLTesterCLI();
};
}
If I do the same in an unmanaged Win32 library, the library is successfully built.
#pragma once
#include <pcl/visualization/pcl_visualizer.h>
class PCLTester
{
public:
PCLTester();
virtual ~PCLTester();
};
Here is the segment from pcl_visualizer.h:
struct FPSCallback : public vtkCommand
{
static FPSCallback *New () { return (new FPSCallback); }
FPSCallback () : actor (), pcl_visualizer (), decimated () {}
FPSCallback (const FPSCallback& src) : vtkCommand (), actor (src.actor), pcl_visualizer (src.pcl_visualizer), decimated (src.decimated) {}
FPSCallback& operator = (const FPSCallback& src) { actor = src.actor; pcl_visualizer = src.pcl_visualizer; decimated = src.decimated; return (*this); }
virtual void
Execute (vtkObject*, unsigned long event_id, void*);
vtkTextActor *actor;
PCLVisualizer* pcl_visualizer;
bool decimated;
};
/** \brief The FPSCallback object for the current visualizer. */
vtkSmartPointer<FPSCallback> update_fps_;
Here is the segment from pcl_visualizer.cpp:
void
pcl::visualization::PCLVisualizer::FPSCallback::Execute (
vtkObject* caller, unsigned long, void*)
{
vtkRenderer *ren = reinterpret_cast<vtkRenderer *> (caller);
float fps = 1.0f / static_cast<float> (ren->GetLastRenderTimeInSeconds ());
char buf[128];
sprintf (buf, "%.1f FPS", fps);
actor->SetInput (buf);
}
Any ideas why the structure and member function conflict in a managed environment?
The reason that my question is unique is that my symbols error was not a result of anything I did: e.g. declaring a function without defining it, having a syntax error that results in the function not being properly defined, or leaving out a .lib dependency from Linker->Input. In my case, I have all the correct .lib files linked and the function from pcl::visualization seems well defined. For some strange reason, it is still being missed at compile time in the managed library. I copied my dependencies from my managed .vcxproj to my unmanaged .vcxproj to verify that it was not a dependency issue. Both classes are setup with the minimum class requirements to prevent conflicts in that regard. Both projects have the same .h file and .lib files linked.
Interestingly, I solved this issue by placing the pcl_visualizer code into my managed c++ code at the top. I had to add a header as well:
#include <vtkTextActor.h>
void
pcl::visualization::PCLVisualizer::FPSCallback::Execute(vtkObject* caller, unsigned long, void*)
{
vtkRenderer *ren = reinterpret_cast<vtkRenderer *> (caller);
float fps = 1.0f / static_cast<float> (ren->GetLastRenderTimeInSeconds());
char buf[128];
sprintf(buf, "%.1f FPS", fps);
actor->SetInput(buf);
}
The other option is to go into pcl_visualizer.h and comment out the offending line (I do not know why this line causes issues, but I narrowed it down to this, and my vtk visualizer still works!):
//FPSCallback (const FPSCallback& src) : vtkCommand (), actor (src.actor), pcl_visualizer (src.pcl_visualizer), decimated (src.decimated) {}
The code then looks like:
struct FPSCallback : public vtkCommand
{
static FPSCallback *New () { return (new FPSCallback); }
FPSCallback () : actor (), pcl_visualizer (), decimated () {}
//FPSCallback (const FPSCallback& src) : vtkCommand (), actor (src.actor), pcl_visualizer (src.pcl_visualizer), decimated (src.decimated) {}
FPSCallback& operator = (const FPSCallback& src) { actor = src.actor; pcl_visualizer = src.pcl_visualizer; decimated = src.decimated; return (*this); }
virtual void
Execute (vtkObject*, unsigned long event_id, void*);
vtkTextActor *actor;
PCLVisualizer* pcl_visualizer;
bool decimated;
};
/** \brief The FPSCallback object for the current visualizer. */
vtkSmartPointer<FPSCallback> update_fps_;

error: expected identifier before '_Bool'

I previously had the following code when dealing with my sync tuple:
static void sync_tuple_changed_callback(const uint32_t key, const Tuple* new_tuple, const Tuple* old_tuple, void* context) {
persist_write_bool(key,new_tuple->value->bool);
}
However, I just tried building this (in Cloud Pebble), and got the error:
../src/main.c: In function 'sync_tuple_changed_callback':
../src/main.c:25:44: error: expected identifier before '_Bool'
What's going on?
There is no bool member of the value union - your best bet is to use the uint8 member instead, passing 1 for true and 0 for false:
static void sync_tuple_changed_callback(const uint32_t key, const Tuple* new_tuple, const Tuple* old_tuple, void* context) {
persist_write_bool(key,new_tuple->value->uint8 != 0);
}

Writing Data to Arduino EEPROM

This is a follow-up to the post here - Writting data to the Arduino's onboard EEPROM
I just tried using the snippets in the URL but wouldn't work. Please help me fix the below error.
write_to_eeprom.cpp:8:5: error: expected unqualified-id before '[' token
write_to_eeprom.cpp: In function 'void setup()':
write_to_eeprom.cpp:12:16: error: 'stringToWrite' was not declared in this scope
write_to_eeprom.cpp: In function 'void loop()':
write_to_eeprom.cpp:22:33: error: invalid conversion from 'uint8_t {aka unsigned char}' to 'char*' [-fpermissive]
write_to_eeprom.cpp: In function 'void EEPROM_write(void*, byte)':
write_to_eeprom.cpp:32:32: error: 'void*' is not a pointer-to-object type
Here is the code
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
char[] stringToWrite = "Test";
void setup() {
lcd.begin(16, 2);
delay(5000);
EEPROM_write(stringToWrite, strlen(stringToWrite));
}
void loop() {
delay(10000);
int addr = 0;
byte datasize = EEPROM.read(addr++);
char stringToRead[0x20]; // allocate enough space for the string here!
char * readLoc = stringToRead;
for (int i=0;i<datasize; i++) {
readLoc = EEPROM.read(addr++);
readLoc++;
}
}
// Function takes a void pointer to data, and how much to write (no other way to know)
// Could also take a starting address, and return the size of the reach chunk, to be more generic
void EEPROM_write(void * data, byte datasize) {
int addr = 0;
EEPROM.write(addr++, datasize);
for (int i=0; i<datasize; i++) {
EEPROM.write(addr++, data[i]);
}
}
Well, you need to fix your code:
line 8 -- [] needs to go AFTER stringToWrite
line 12 -- should get better after fixing line 8
line 22 -- you need to dereference readLoc. add a '*' before it.
line 32 -- your parameter "data" is a pointer to void, which has no size. Because of that, you will not be able to use it as an array. You could change the declaration to:
void EEPROM_write(char * data, byte datasize)
That fixes the compiler errors. Taking a quick look at the semantics of the code seems to be doing what you want. Good luck.

Resources