Some cleanups in disconnect

This commit is contained in:
Isaac Connor 2021-02-16 10:32:07 -05:00
parent a56d78bb04
commit a82247b105
1 changed files with 23 additions and 42 deletions

View File

@ -1021,52 +1021,46 @@ bool Monitor::connect() {
} // Monitor::connect } // Monitor::connect
bool Monitor::disconnect() { bool Monitor::disconnect() {
if ( !mem_ptr ) if (mem_ptr == nullptr)
return true; return true;
#if ZM_MEM_MAPPED #if ZM_MEM_MAPPED
if ( mem_ptr > (void *)0 ) {
msync(mem_ptr, mem_size, MS_ASYNC); msync(mem_ptr, mem_size, MS_ASYNC);
munmap(mem_ptr, mem_size); munmap(mem_ptr, mem_size);
} if (map_fd >= 0) close(map_fd);
if ( map_fd >= 0 )
close(map_fd);
map_fd = -1; map_fd = -1;
mem_ptr = nullptr; mem_ptr = nullptr;
shared_data = nullptr; shared_data = nullptr;
if ( purpose == CAPTURE ) { if (purpose == CAPTURE) {
if ( unlink(mem_file) < 0 ) { if (unlink(mem_file) < 0) {
Warning("Can't unlink '%s': %s", mem_file, strerror(errno)); Warning("Can't unlink '%s': %s", mem_file, strerror(errno));
} }
} }
#else // ZM_MEM_MAPPED #else // ZM_MEM_MAPPED
struct shmid_ds shm_data; struct shmid_ds shm_data;
if ( shmctl(shm_id, IPC_STAT, &shm_data) < 0 ) { if (shmctl(shm_id, IPC_STAT, &shm_data) < 0) {
Debug(3, "Can't shmctl: %s", strerror(errno)); Debug(3, "Can't shmctl: %s", strerror(errno));
return false; return false;
} }
shm_id = 0; shm_id = 0;
if ( shm_data.shm_nattch <= 1 ) { if (shm_data.shm_nattch <= 1) {
if ( shmctl(shm_id, IPC_RMID, 0) < 0 ) { if (shmctl(shm_id, IPC_RMID, 0) < 0) {
Debug(3, "Can't shmctl: %s", strerror(errno)); Debug(3, "Can't shmctl: %s", strerror(errno));
return false; return false;
} }
} }
if ( shmdt(mem_ptr) < 0 ) { if (shmdt(mem_ptr) < 0) {
Debug(3, "Can't shmdt: %s", strerror(errno)); Debug(3, "Can't shmdt: %s", strerror(errno));
return false; return false;
} }
#endif // ZM_MEM_MAPPED #endif // ZM_MEM_MAPPED
if ( event ) {
Info( "%s: image_count:%d - Closing event %" PRIu64 ", shutting down", name, image_count, event->Id() ); if (image_buffer) {
closeEvent();
}
if ( image_buffer ) {
for ( int i = 0; i < image_buffer_count; i++ ) { for ( int i = 0; i < image_buffer_count; i++ ) {
// We delete the image because it is an object pointing to space that won't be free'd. // We delete the image because it is an object pointing to space that won't be free'd.
delete image_buffer[i].image; delete image_buffer[i].image;
@ -1078,38 +1072,33 @@ bool Monitor::disconnect() {
image_buffer = nullptr; image_buffer = nullptr;
} }
return true; return true;
} // end bool Monitor::disconnect() } // end bool Monitor::disconnect()
Monitor::~Monitor() { Monitor::~Monitor() {
Close(); Close();
if ( mem_ptr ) { if (mem_ptr != nullptr) {
if ( event ) { std::lock_guard<std::mutex> lck(event_mutex);
Info( "%s: image_count:%d - Closing event %" PRIu64 ", shutting down", name, image_count, event->Id() ); if (event) {
Info("%s: image_count:%d - Closing event %" PRIu64 ", shutting down", name, image_count, event->Id());
closeEvent(); closeEvent();
} }
if ( purpose == ANALYSIS ) { if (purpose != QUERY) {
shared_data->state = state = IDLE; shared_data->state = state = IDLE;
// I think we set it to the count so that it is technically 1 behind capture, which starts at 0
shared_data->last_read_index = image_buffer_count; shared_data->last_read_index = image_buffer_count;
shared_data->last_read_time = 0; shared_data->last_read_time = 0;
if ( Event::PreAlarmCount() )
Event::EmptyPreAlarmFrames();
} else if ( purpose == CAPTURE ) {
shared_data->valid = false; shared_data->valid = false;
memset(mem_ptr, 0, mem_size); memset(mem_ptr, 0, mem_size);
if ( (deinterlacing & 0xff) == 4 ) { if ( (deinterlacing & 0xff) == 4 ) {
delete next_buffer.image; delete next_buffer.image;
delete next_buffer.timestamp; delete next_buffer.timestamp;
} }
} } // end if purpose != query
disconnect(); disconnect();
} // end if mem_ptr } // end if mem_ptr
if ( analysis_it ) { if (analysis_it) {
packetqueue.free_it(analysis_it); packetqueue.free_it(analysis_it);
analysis_it = nullptr; analysis_it = nullptr;
} }
@ -1120,7 +1109,7 @@ Monitor::~Monitor() {
delete[] zones; delete[] zones;
delete storage; delete storage;
if ( n_linked_monitors ) { if (n_linked_monitors) {
for ( int i=0; i < n_linked_monitors; i++ ) { for ( int i=0; i < n_linked_monitors; i++ ) {
delete linked_monitors[i]; delete linked_monitors[i];
} }
@ -2950,18 +2939,10 @@ int Monitor::PrimeCapture() {
int Monitor::PreCapture() const { return camera->PreCapture(); } int Monitor::PreCapture() const { return camera->PreCapture(); }
int Monitor::PostCapture() const { return camera->PostCapture(); } int Monitor::PostCapture() const { return camera->PostCapture(); }
int Monitor::Close() { int Monitor::Close() {
if (camera) if (camera) camera->Close();
camera->Close();
packetqueue.clear(); packetqueue.clear();
#if 0
if ( packetqueue ) {
delete packetqueue;
packetqueue = nullptr;
analysis_it = nullptr; // deleted by packetqueue
}
#endif
return 1; return 1;
}; }
Monitor::Orientation Monitor::getOrientation() const { return orientation; } Monitor::Orientation Monitor::getOrientation() const { return orientation; }
// Wait for camera to get an image, and then assign it as the base reference image. // Wait for camera to get an image, and then assign it as the base reference image.