Push locked packets into event packetqueue instead of unlcoked contents. Prevents freeing of data before writing to event, hence crashing.

This commit is contained in:
Isaac Connor 2022-01-28 15:38:18 -05:00
parent fe8747e5e7
commit e5792c21c9
3 changed files with 37 additions and 35 deletions

View File

@ -301,9 +301,9 @@ void Event::updateNotes(const StringSetMap &newNoteSetMap) {
} // end if update } // end if update
} // void Event::updateNotes(const StringSetMap &newNoteSetMap) } // void Event::updateNotes(const StringSetMap &newNoteSetMap)
void Event::AddPacket(const std::shared_ptr<ZMPacket>&packet) { void Event::AddPacket(ZMLockedPacket *packetlock) {
std::unique_lock<std::mutex> lck(packet_queue_mutex); std::unique_lock<std::mutex> lck(packet_queue_mutex);
packet_queue.push(packet); packet_queue.push(packetlock);
packet_queue_condition.notify_one(); packet_queue_condition.notify_one();
} }
@ -684,7 +684,9 @@ void Event::Run() {
while (true) { while (true) {
if (!packet_queue.empty()) { if (!packet_queue.empty()) {
Debug(1, "adding packet"); Debug(1, "adding packet");
this->AddPacket_(packet_queue.front()); const ZMLockedPacket * packet_lock = packet_queue.front();
this->AddPacket_(packet_lock->packet_);
delete packet_lock;
packet_queue.pop(); packet_queue.pop();
} else { } else {
if (terminate_ or zm_terminate) { if (terminate_ or zm_terminate) {

View File

@ -105,7 +105,7 @@ class Event {
void createNotes(std::string &notes); void createNotes(std::string &notes);
std::queue<std::shared_ptr<ZMPacket>> packet_queue; std::queue<ZMLockedPacket *> packet_queue;
std::mutex packet_queue_mutex; std::mutex packet_queue_mutex;
std::condition_variable packet_queue_condition; std::condition_variable packet_queue_condition;
@ -134,7 +134,7 @@ class Event {
SystemTimePoint EndTime() const { return end_time; } SystemTimePoint EndTime() const { return end_time; }
TimePoint::duration Duration() const { return end_time - start_time; }; TimePoint::duration Duration() const { return end_time - start_time; };
void AddPacket(const std::shared_ptr<ZMPacket> &p); void AddPacket(ZMLockedPacket *);
void AddPacket_(const std::shared_ptr<ZMPacket> &p); void AddPacket_(const std::shared_ptr<ZMPacket> &p);
bool WritePacket(const std::shared_ptr<ZMPacket> &p); bool WritePacket(const std::shared_ptr<ZMPacket> &p);
bool SendFrameImage(const Image *image, bool alarm_frame=false); bool SendFrameImage(const Image *image, bool alarm_frame=false);

View File

@ -2270,8 +2270,7 @@ bool Monitor::Analyse() {
shared_data->state = state = IDLE; shared_data->state = state = IDLE;
} // end if ( trigger_data->trigger_state != TRIGGER_OFF ) } // end if ( trigger_data->trigger_state != TRIGGER_OFF )
if (event) event->AddPacket(snap); if (event) event->AddPacket(packet_lock);
} // end scope for event_lock
// In the case where people have pre-alarm frames, the web ui will generate the frame images // In the case where people have pre-alarm frames, the web ui will generate the frame images
// from the mp4. So no one will notice anyways. // from the mp4. So no one will notice anyways.
@ -2297,7 +2296,8 @@ bool Monitor::Analyse() {
analysis_image_count++; analysis_image_count++;
} }
packetqueue.increment_it(analysis_it); packetqueue.increment_it(analysis_it);
delete packet_lock; if (!event) delete packet_lock;
} // end scope for event_lock
//packetqueue.unlock(packet_lock); //packetqueue.unlock(packet_lock);
shared_data->last_read_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); shared_data->last_read_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
@ -2870,16 +2870,16 @@ Event * Monitor::openEvent(
// Write out starting packets, do not modify packetqueue it will garbage collect itself // Write out starting packets, do not modify packetqueue it will garbage collect itself
while (starting_packet and ((*start_it) != *analysis_it)) { while (starting_packet and ((*start_it) != *analysis_it)) {
event->AddPacket(starting_packet); event->AddPacket(starting_packet_lock);
// Have added the packet, don't want to unlock it until we have locked the next // Have added the packet, don't want to unlock it until we have locked the next
packetqueue.increment_it(start_it); packetqueue.increment_it(start_it);
if ((*start_it) == *analysis_it) { if ((*start_it) == *analysis_it) {
if (starting_packet_lock) delete starting_packet_lock; //if (starting_packet_lock) delete starting_packet_lock;
break; break;
} }
ZMLockedPacket *lp = packetqueue.get_packet(start_it); ZMLockedPacket *lp = packetqueue.get_packet(start_it);
delete starting_packet_lock; //delete starting_packet_lock;
if (!lp) return nullptr; // only on terminate FIXME if (!lp) return nullptr; // only on terminate FIXME
starting_packet_lock = lp; starting_packet_lock = lp;
starting_packet = lp->packet_; starting_packet = lp->packet_;