diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index a646f4952..1793b3b0f 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -15,6 +15,7 @@ AnalysisThread::~AnalysisThread() { } void AnalysisThread::Start() { + Stop(); // Signal any running thread to terminate first if (thread_.joinable()) thread_.join(); terminate_ = false; Debug(3, "Starting analysis thread"); diff --git a/src/zm_camera.cpp b/src/zm_camera.cpp index e9c7e5b1a..62276b6c3 100644 --- a/src/zm_camera.cpp +++ b/src/zm_camera.cpp @@ -86,8 +86,13 @@ Camera::~Camera() { AVStream *Camera::getVideoStream() { if ( !mVideoStream ) { - if ( !mFormatContext ) + if ( !mFormatContext ) { mFormatContext = avformat_alloc_context(); + if ( !mFormatContext ) { + Error("Failed to allocate AVFormatContext"); + return nullptr; + } + } Debug(1, "Allocating avstream"); mVideoStream = avformat_new_stream(mFormatContext, nullptr); if ( mVideoStream ) { diff --git a/src/zm_comms.cpp b/src/zm_comms.cpp index f827bbc25..5d5496948 100644 --- a/src/zm_comms.cpp +++ b/src/zm_comms.cpp @@ -556,12 +556,13 @@ bool zm::InetSocket::bind(const char *host, const char *serv) { mSd = -1; } + freeaddrinfo(result); /* No longer needed */ + if (rp == nullptr) { /* No address succeeded */ Error("bind(), Could not bind"); return false; } - freeaddrinfo(result); /* No longer needed */ return true; } @@ -652,7 +653,7 @@ bool zm::Select::addReader(CommsBase *comms) { bool zm::Select::deleteReader(CommsBase *comms) { if (!comms->isOpen()) { - Error("Unable to add closed reader"); + Error("Unable to delete closed reader"); return false; } if (mReaders.erase(comms)) { @@ -664,7 +665,7 @@ bool zm::Select::deleteReader(CommsBase *comms) { void zm::Select::clearReaders() { mReaders.clear(); - mMaxFd = -1; + calcMaxFd(); } bool zm::Select::addWriter(CommsBase *comms) { @@ -687,7 +688,7 @@ bool zm::Select::deleteWriter(CommsBase *comms) { void zm::Select::clearWriters() { mWriters.clear(); - mMaxFd = -1; + calcMaxFd(); } int zm::Select::wait() { diff --git a/src/zm_config.cpp b/src/zm_config.cpp index ce48ed9d3..0a496e68d 100644 --- a/src/zm_config.cpp +++ b/src/zm_config.cpp @@ -91,12 +91,12 @@ void zmLoadDBConfig() { } else { Fatal("Can't get ServerName for Server ID %d", staticConfig.SERVER_ID); } + } - if (staticConfig.SERVER_ID) { - Debug(3, "Multi-server configuration detected. Server is %d.", staticConfig.SERVER_ID); - } else { - Debug(3, "Single server configuration assumed because no Server ID or Name was specified."); - } + if (staticConfig.SERVER_ID) { + Debug(3, "Multi-server configuration detected. Server is %d.", staticConfig.SERVER_ID); + } else { + Debug(3, "Single server configuration assumed because no Server ID or Name was specified."); } staticConfig.capture_file_format = stringtf("%%s/%%0%dd-capture.jpg", config.event_image_digits); @@ -148,7 +148,7 @@ void process_configfile(char const *configFile) { do { *temp_ptr = '\0'; temp_ptr--; - } while ( *temp_ptr == ' ' || *temp_ptr == '\t' ); + } while ( temp_ptr >= name_ptr && (*temp_ptr == ' ' || *temp_ptr == '\t') ); // Remove leading white space and leading quotes from the value part white_len = strspn(val_ptr, " \t"); diff --git a/src/zm_db.cpp b/src/zm_db.cpp index a0acd7523..e9422926d 100644 --- a/src/zm_db.cpp +++ b/src/zm_db.cpp @@ -64,7 +64,7 @@ bool zmDbConnect() { } else { std::string dbHost = staticConfig.DB_HOST.substr(0, colonIndex); std::string dbPortOrSocket = staticConfig.DB_HOST.substr(colonIndex+1); - if ( dbPortOrSocket[0] == '/' ) { + if ( !dbPortOrSocket.empty() && dbPortOrSocket[0] == '/' ) { if ( !mysql_real_connect( &dbconn, nullptr, @@ -211,11 +211,15 @@ int zmDbDo(const std::string &query) { // If we failed. Sleeping 1 sec may be way too much. sleep(1); } - if (zm_terminate) return 0; + if (zm_terminate) { + logger->databaseLevel(oldLevel); + return 0; + } } else { // Not a connection error Error("Can't run query %s: %d %s", query.c_str(), rc, reason.c_str()); if (mysql_errno(&dbconn) != ER_LOCK_WAIT_TIMEOUT) { + logger->databaseLevel(oldLevel); return rc; } } // end if !connected diff --git a/src/zm_decoder_thread.cpp b/src/zm_decoder_thread.cpp index fa36f06b9..952c63c43 100644 --- a/src/zm_decoder_thread.cpp +++ b/src/zm_decoder_thread.cpp @@ -14,6 +14,7 @@ DecoderThread::~DecoderThread() { } void DecoderThread::Start() { + Stop(); // Signal any running thread to terminate first if (thread_.joinable()) thread_.join(); terminate_ = false; thread_ = std::thread(&DecoderThread::Run, this); diff --git a/src/zm_event.cpp b/src/zm_event.cpp index 995fe31f6..557ba2ffe 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -227,10 +227,10 @@ Event::~Event() { uint64_t video_size = 0; DIR *video_dir; if ((video_dir = opendir(path.c_str())) != NULL) { - struct dirent *video_file; - while ((video_file = readdir(video_dir)) != NULL) { + struct dirent *dir_entry; + while ((dir_entry = readdir(video_dir)) != NULL) { struct stat vf_stat; - if (stat((path + "/" + video_file->d_name).c_str(), &vf_stat) == 0 && + if (stat((path + "/" + dir_entry->d_name).c_str(), &vf_stat) == 0 && S_ISREG(vf_stat.st_mode)) video_size += vf_stat.st_size; } @@ -392,7 +392,7 @@ void Event::AddPacket_(const std::shared_ptrpacket) { Tag *tag = nullptr; auto tag_it = tags.find(cls); if (tag_it == tags.end()) { - Debug(1, "Tag not foudn %s", cls.c_str()); + Debug(1, "Tag not found %s", cls.c_str()); tag = Tag::find(cls); if (!tag) { tag = new Tag(); @@ -400,10 +400,13 @@ void Event::AddPacket_(const std::shared_ptrpacket) { tag->save(); Debug(1, "Created new Tag %s", cls.c_str()); tags.emplace(std::make_pair(cls, *tag)); + int tag_id = tag->Id(); + delete tag; // Delete after copying into map + tag = nullptr; - if (tag->Id()) { - // Store - Event_Tag event_tag(tag->Id(), id, packet->timestamp); + if (tag_id) { + // Store + Event_Tag event_tag(tag_id, id, packet->timestamp); event_tag.save(); } } else { diff --git a/src/zm_event_tag.cpp b/src/zm_event_tag.cpp index 35650ac1a..3e4d6ac59 100644 --- a/src/zm_event_tag.cpp +++ b/src/zm_event_tag.cpp @@ -46,7 +46,8 @@ Event_Tag::Event_Tag(const MYSQL_ROW &dbrow) { tag_id = atoll(dbrow[index++]); event_id = atoll(dbrow[index++]); assigned_on = StringToSystemTimePoint(dbrow[index++]); - assigned_by = atoi(dbrow[index++]); + assigned_by = dbrow[index] ? atoi(dbrow[index]) : 0; + index++; } Event_Tag::Event_Tag(uint64_t p_tag_id, uint64_t p_event_id, SystemTimePoint p_assigned_on, unsigned int p_assigned_by ) : diff --git a/src/zm_eventstream.cpp b/src/zm_eventstream.cpp index e55f7555a..47bab24a7 100644 --- a/src/zm_eventstream.cpp +++ b/src/zm_eventstream.cpp @@ -55,10 +55,12 @@ bool EventStream::loadInitialEventData(int monitor_id, SystemTimePoint event_tim if ( mysql_errno(&dbconn) ) { Error("Can't fetch row: %s", mysql_error(&dbconn)); + mysql_free_result(result); return false; } if (!mysql_num_rows(result)) { Error("Unable to load event using %s", sql.c_str()); + mysql_free_result(result); return false; } @@ -277,7 +279,7 @@ bool EventStream::loadEventData(uint64_t event_id) { // Fill in data between bulk frames if (id_diff > 1) { for (int i = last_id + 1; i < id; i++) { - auto frame = event_data->frames.emplace_back( + auto &frame = event_data->frames.emplace_back( i, last_timestamp + ((i - last_id) * delta), std::chrono::duration_cast((last_frame->timestamp - event_data->start_time) + delta), @@ -293,7 +295,7 @@ bool EventStream::loadEventData(uint64_t event_id) { frame.in_db); } } - auto frame = event_data->frames.emplace_back(id, timestamp, offset, delta, true); + auto &frame = event_data->frames.emplace_back(id, timestamp, offset, delta, true); last_frame = &frame; last_id = id; last_offset = offset; @@ -311,7 +313,7 @@ bool EventStream::loadEventData(uint64_t event_id) { if (!last_frame) { // There were no frames in db delta = Microseconds( static_cast(1000000 * base_fps / FPSeconds(event_data->duration).count()) ); - auto frame = event_data->frames.emplace_back( + auto &frame = event_data->frames.emplace_back( 1, event_data->start_time, Microseconds(0), @@ -339,7 +341,7 @@ bool EventStream::loadEventData(uint64_t event_id) { if (event_data->end_time < last_timestamp) break; last_id ++; - auto frame = event_data->frames.emplace_back( + auto &frame = event_data->frames.emplace_back( last_id, last_timestamp, last_frame->offset + delta,