 la_mojo: fix memory leaks and heap corruption
Continuous leaks:
- protocolParser: on package-pool overflow the oldest package was removed
but never freed while the newest (still pooled) one was freed. Every
packet a handler does not consume (e.g. other sensors on a shared bus)
leaked once the pool was full. Free the removed package, free pooled
packages in the destructor and getPackage(), use memmove for the
overlapping buffer shifts.
- SQLite_Connector::Query: loop only while sqlite3_step() returns
SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on
SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed
open, fix escapeBinary() writing into uninitialised buffers.
- basicThread/basicDualThread: threads that ended by themselves were never
joined, leaking their stacks on every stream drop/restart. Always join
once (detach when called from the thread itself); check pthread_create
result correctly.
- curlRequest: free the header list; initialise m_pPostData and the
content type; curlRequestsManager deletes requests it cannot queue.
- kernelModuleModemManager: free the phonebook ID list on every reload.
- deviceNodeFactory: do not append the same pending node every cycle.
- streamHandler: free frames left in the encoding queue on destruction.
Heap corruption:
- kernelModuleFactory: module that failed to start was deleted twice.
- mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a
bool; compare with -1. Fix packetExtractor and destructor leaks.
- utilizeBasicEvent: payloads are malloc'ed, release with free().
- videoAVLIBSource: avoid sws context double free, NULL check order.
- trim(): memmove for overlapping copy.
Smaller leaks: dropSession duplicate query, LRF upload data, report
backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
4 days ago  la_mojo nodes and modules: register for events only when fully built
A handler was registered in the basicEventHandler constructor (before
the derived members existed) and removed in its destructor (after they
were destroyed): the event heap could call dispatchEvent() on a half-
built or half-destroyed node or module when one was created or deleted
while la_mojo runs (node / module reload).
- Camera nodes (deviceNodePTZCamControl, deviceNodePresetCamControl,
deviceNodeStaticCamControl): basicEventHandler(false), attachEvents()
at the end of both constructors, detachEvents() first thing in the
destructor.
- Kernel modules: kernelModule no longer registers in its constructor;
kernelModuleFactory attaches a module right after creating it (all
modules are created there), before startService(). Every module
destructor (USB relay, Kafka, FlyHunter, SSDP, ONVIF observer, embed
video (both variants), modem manager, Bosch, attention cloud, report
backupper) calls detachEvents() first.
- basicEventHeap::moveHandlerTop/Bottom no longer register a handler
that is not registered (would have registered an object twice once
attachEvents() runs).
Sensor nodes are not event handlers and are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
19 hours ago |
|
#include "kernelModuleReportBackupper.h"
#include "../../components/base/basicFunctionality.h"
#define LOG(X) kernelModule::m_pLog->Log("MOD_REPBACK", this, X)
#ifdef DEBUG
#define LOG_ERROR(X) \
{ \ char _tBfrErrMSG2w34[2048]; \ sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \ kernelModule::m_pLog->Log("MOD_REPBACK", this, _LOG_ERROR, _tBfrErrMSG2w34); \ }#else
#define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_REPBACK", this, _LOG_ERROR, X);
#endif
kernelModuleReportBackupper::kernelModuleReportBackupper(uint32_t moduleID) : kernelModule(moduleID, KERNEL_MODULE_REPORT_BACKUPPER){ DBC_REPORT_BACKUP_SETTINGS * tData = m_pDBC->getReportBackupData(m_nDBAUXID); if (tData) { memcpy(&m_sBackupSettings, tData, sizeof(DBC_REPORT_BACKUP_SETTINGS)); free(tData); // returned by getReportBackupData(), owned by the caller
} else { memset(&m_sBackupSettings, 0, sizeof(DBC_REPORT_BACKUP_SETTINGS)); }; m_bBackupIsWorking = false;}
kernelModuleReportBackupper::~kernelModuleReportBackupper() { detachEvents(); // stop event delivery before the members are destroyed (see basicEventHandler.h)
}
bool kernelModuleReportBackupper::validate(){ if (m_sBackupSettings.comulative_time) { m_mFlagsProtector.lock(); bool bBIW = m_bBackupIsWorking; m_mFlagsProtector.unlock(); if (! bBIW) { // Backups Enabled
unsigned long long tTS = getTimeValue(); uint32_t cTS = tTS / 1000; cTS = (cTS / 86400) * 86400; // Have Rounded to a days - 1;
if ((m_sBackupSettings.last_backup_time == 0) || ((cTS - m_sBackupSettings.last_backup_time) > m_sBackupSettings.comulative_time)) { m_mFlagsProtector.lock(); m_bBackupIsWorking = true; m_mFlagsProtector.unlock(); // Do Backup Now
LOG("Starting Backup ..."); doReportBackup(cTS); }; }; }; return true;}
// https://developers.google.com/gdata/articles/using_cURL
void kernelModuleReportBackupper::doReportBackup(uint32_t cts){ if (m_sBackupSettings.last_backup_time == 0) { m_sBackupSettings.last_backup_time = m_pDBC->getFirstEventTS(); }; for (uint32_t ddi = m_sBackupSettings.last_backup_time; ddi < cts; ddi += 86400) { char cPATH[MAX_PATH]; char cCMD[2048]; CSTR cFUID = getUniqID_MD5(); if (cFUID) { REPORT_DIRINFO tRDI; fillRDF(cts, tRDI); sprintf(cPATH, "%s/data/reports/%s/%s/%s", getcwd(cCMD, MAX_PATH), tRDI.YEAR, tRDI.MONTH, tRDI.DAY); sprintf(cCMD, "tar -cvjf /tmp/%s.tar.bz2 --absolute-names -C %s . ", cFUID, cPATH); LOG(cCMD); system(cCMD); sprintf(cCMD, "/tmp/%s.tar.bz2", cFUID); free(cFUID); if (fileExist(cCMD)) { // delete report
}; }; }; m_sBackupSettings.last_backup_time = cts; // m_pDBC->updateLastBackupTime( cts );
}
#undef LOG
#undef LOG_ERROR
|