work on RAII and memory leaks

This commit is contained in:
mrkraimer
2016-06-01 10:10:00 -04:00
parent 1eea87efc7
commit e025e542ea
15 changed files with 701 additions and 287 deletions

View File

@@ -28,7 +28,6 @@ PVRecordPtr PVRecord::create(
{
PVRecordPtr pvRecord(new PVRecord(recordName,pvStructure));
if(!pvRecord->init()) {
pvRecord->destroy();
pvRecord.reset();
}
return pvRecord;
@@ -52,13 +51,14 @@ PVRecord::~PVRecord()
if(traceLevel>1) {
cout << "~PVRecord() " << recordName << endl;
}
destroy();
}
void PVRecord::initPVRecord()
{
PVRecordStructurePtr parent;
pvRecordStructure = PVRecordStructurePtr(
new PVRecordStructure(pvStructure,parent,getPtrSelf()));
new PVRecordStructure(pvStructure,parent,shared_from_this()));
pvRecordStructure->init();
PVFieldPtr pvField = pvStructure->getSubField("timeStamp");
if(pvField) pvTimeStamp.attach(pvField);
@@ -76,31 +76,26 @@ void PVRecord::destroy()
isDestroyed = true;
pvTimeStamp.detach();
std::list<PVRecordClientWPtr>::iterator clientIter;
while(true) {
clientIter = pvRecordClientList.begin();
if(clientIter==pvRecordClientList.end()) break;
PVRecordClientPtr client = clientIter->lock();
if(client.get()) {
for(std::list<PVRecordClientWPtr>::iterator iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(client) {
epicsGuardRelease<epics::pvData::Mutex> unguard(guard);
client->detach(getPtrSelf());
client->detach(shared_from_this());
}
pvRecordClientList.erase(clientIter);
}
std::list<PVListenerWPtr>::iterator listenerIter;
while(true) {
listenerIter = pvListenerList.begin();
if(listenerIter==pvListenerList.end()) break;
PVListenerPtr listener = listenerIter->lock();
if(listener.get()) {
for(std::list<PVListenerWPtr>::iterator iter = pvListenerList.begin();
iter!=pvListenerList.end();
iter++ )
{
PVListenerPtr listener = iter->lock();
if(listener) {
epicsGuardRelease<epics::pvData::Mutex> unguard(guard);
listener->unlisten(getPtrSelf());
listener->unlisten(shared_from_this());
}
pvListenerList.erase(listenerIter);
}
pvRecordStructure->destroy();
pvRecordStructure.reset();
pvStructure.reset();
}
void PVRecord::process()
@@ -188,7 +183,7 @@ void PVRecord::lockOtherRecord(PVRecordPtr const & otherRecord)
bool PVRecord::addPVRecordClient(PVRecordClientPtr const & pvRecordClient)
{
if(traceLevel>2) {
if(traceLevel>1) {
cout << "PVRecord::addPVRecordClient() " << recordName << endl;
}
epicsGuard<epics::pvData::Mutex> guard(mutex);
@@ -212,7 +207,7 @@ bool PVRecord::addPVRecordClient(PVRecordClientPtr const & pvRecordClient)
bool PVRecord::removePVRecordClient(PVRecordClientPtr const & pvRecordClient)
{
if(traceLevel>2) {
if(traceLevel>1) {
cout << "PVRecord::removePVRecordClient() " << recordName << endl;
}
epicsGuard<epics::pvData::Mutex> guard(mutex);
@@ -251,7 +246,7 @@ void PVRecord::detachClients()
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
epicsGuardRelease <epics::pvData::Mutex> unguard(guard);
client->detach(getPtrSelf());
client->detach(shared_from_this());
}
pvRecordClientList.clear();
}
@@ -279,7 +274,7 @@ bool PVRecord::addListener(
pvListenerList.push_back(pvListener);
this->pvListener = pvListener;
isAddListener = true;
pvCopy->traverseMaster(getPtrSelf());
pvCopy->traverseMaster(shared_from_this());
this->pvListener = PVListenerPtr();;
return true;
}
@@ -316,7 +311,7 @@ bool PVRecord::removeListener(
pvListenerList.erase(iter);
this->pvListener = pvListener;
isAddListener = false;
pvCopy->traverseMaster(getPtrSelf());
pvCopy->traverseMaster(shared_from_this());
this->pvListener = PVListenerPtr();
return true;
}
@@ -335,7 +330,7 @@ void PVRecord::beginGroupPut()
{
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
listener->beginGroupPut(getPtrSelf());
listener->beginGroupPut(shared_from_this());
}
}
@@ -350,7 +345,7 @@ void PVRecord::endGroupPut()
{
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
listener->endGroupPut(getPtrSelf());
listener->endGroupPut(shared_from_this());
}
}
@@ -377,8 +372,8 @@ PVRecordField::PVRecordField(
void PVRecordField::init()
{
fullFieldName = pvField->getFieldName();
PVRecordStructurePtr pvParent = parent;
fullFieldName = pvField.lock()->getFieldName();
PVRecordStructurePtr pvParent(parent.lock());
while(pvParent) {
string parentName = pvParent->getPVField()->getFieldName();
if(parentName.size()>0) {
@@ -387,39 +382,32 @@ void PVRecordField::init()
}
pvParent = pvParent->getParent();
}
PVRecordPtr pvRecord(this->pvRecord.lock());
if(fullFieldName.size()>0) {
fullName = pvRecord->getRecordName() + '.' + fullFieldName;
} else {
fullName = pvRecord->getRecordName();
}
pvField->setPostHandler(getPtrSelf());
pvField.lock()->setPostHandler(shared_from_this());
}
PVRecordField::~PVRecordField()
PVRecordStructurePtr PVRecordField::getParent()
{
return parent.lock();
}
void PVRecordField::destroy()
{
pvRecord.reset();
parent.reset();
pvField.reset();
pvListenerList.clear();
}
PVRecordStructurePtr PVRecordField::getParent() {return parent;}
PVFieldPtr PVRecordField::getPVField() {return pvField;}
PVFieldPtr PVRecordField::getPVField() {return pvField.lock();}
string PVRecordField::getFullFieldName() {return fullFieldName; }
string PVRecordField::getFullName() {return fullName; }
PVRecordPtr PVRecordField::getPVRecord() {return pvRecord;}
PVRecordPtr PVRecordField::getPVRecord() {return pvRecord.lock();}
bool PVRecordField::addListener(PVListenerPtr const & pvListener)
{
if(pvRecord->getTraceLevel()>1) {
PVRecordPtr pvRecord(this->pvRecord.lock());
if(pvRecord && pvRecord->getTraceLevel()>1) {
cout << "PVRecordField::addListener() " << getFullName() << endl;
}
std::list<PVListenerWPtr>::iterator iter;
@@ -436,7 +424,8 @@ bool PVRecordField::addListener(PVListenerPtr const & pvListener)
void PVRecordField::removeListener(PVListenerPtr const & pvListener)
{
if(pvRecord->getTraceLevel()>1) {
PVRecordPtr pvRecord(this->pvRecord.lock());
if(pvRecord && pvRecord->getTraceLevel()>1) {
cout << "PVRecordField::removeListener() " << getFullName() << endl;
}
std::list<PVListenerWPtr>::iterator iter;
@@ -452,15 +441,16 @@ void PVRecordField::removeListener(PVListenerPtr const & pvListener)
void PVRecordField::postPut()
{
PVRecordStructurePtr parent(this->parent.lock());;
if(parent) {
parent->postParent(getPtrSelf());
parent->postParent(shared_from_this());
}
postSubField();
}
void PVRecordField::postParent(PVRecordFieldPtr const & subField)
{
PVRecordStructurePtr pvrs = static_pointer_cast<PVRecordStructure>(getPtrSelf());
PVRecordStructurePtr pvrs = static_pointer_cast<PVRecordStructure>(shared_from_this());
std::list<PVListenerWPtr>::iterator iter;
for(iter = pvListenerList.begin(); iter != pvListenerList.end(); ++iter)
{
@@ -468,6 +458,7 @@ void PVRecordField::postParent(PVRecordFieldPtr const & subField)
if(!listener.get()) continue;
listener->dataPut(pvrs,subField);
}
PVRecordStructurePtr parent(this->parent.lock());
if(parent) parent->postParent(subField);
}
@@ -475,7 +466,7 @@ void PVRecordField::postSubField()
{
callListener();
if(isStructure) {
PVRecordStructurePtr pvrs = static_pointer_cast<PVRecordStructure>(getPtrSelf());
PVRecordStructurePtr pvrs = static_pointer_cast<PVRecordStructure>(shared_from_this());
PVRecordFieldPtrArrayPtr pvRecordFields = pvrs->getPVRecordFields();
PVRecordFieldPtrArray::iterator iter;
for(iter = pvRecordFields->begin() ; iter !=pvRecordFields->end(); iter++) {
@@ -490,7 +481,7 @@ void PVRecordField::callListener()
for (iter = pvListenerList.begin(); iter!=pvListenerList.end(); iter++ ) {
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
listener->dataPut(getPtrSelf());
listener->dataPut(shared_from_this());
}
}
@@ -505,30 +496,14 @@ PVRecordStructure::PVRecordStructure(
{
}
PVRecordStructure::~PVRecordStructure()
{
}
void PVRecordStructure::destroy()
{
PVRecordFieldPtrArray::iterator iter;
PVRecordField::destroy();
for(iter = pvRecordFields->begin() ; iter !=pvRecordFields->end(); iter++) {
(*iter)->destroy();
}
PVRecordField::destroy();
pvRecordFields.reset();
pvStructure.reset();
}
void PVRecordStructure::init()
{
PVRecordField::init();
const PVFieldPtrArray & pvFields = pvStructure->getPVFields();
const PVFieldPtrArray & pvFields = pvStructure.lock()->getPVFields();
size_t numFields = pvFields.size();
pvRecordFields->reserve( numFields);
PVRecordStructurePtr self =
static_pointer_cast<PVRecordStructure>(getPtrSelf());
static_pointer_cast<PVRecordStructure>(shared_from_this());
PVRecordPtr pvRecord = getPVRecord();
for(size_t i=0; i<numFields; i++) {
PVFieldPtr pvField = pvFields[i];
@@ -552,7 +527,7 @@ PVRecordFieldPtrArrayPtr PVRecordStructure::getPVRecordFields()
return pvRecordFields;
}
PVStructurePtr PVRecordStructure::getPVStructure() {return pvStructure;}
PVStructurePtr PVRecordStructure::getPVStructure() {return pvStructure.lock();}
void PVRecordStructure::removeListener(PVListenerPtr const & pvListener)
{