在分配的内存之外写

时间:2015-07-02 09:55:04

标签: c++ qt memory-management access-violation

Valgrind告诉我,我的应用程序是在分配的内存之外写的。这可能是什么原因?输出如下:

==18307== 1 errors in context 3 of 5:
==18307== Invalid write of size 8
==18307==    at 0x65A35F0: QDateTime::QDateTime() (in /usr/lib64/libQtCore.so.4.8.6)
==18307==    by 0xB752F5: ora::Patient::Patient(ora::Site*)(oraPatient.cxx:53)
==18307==    by 0xB359F2: main (main.cxx:32)
==18307==  Address 0x16b3b680 is 8 bytes after a block of size 328 alloc'd
==18307==    at 0x4C29118: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==18307==    by 0xB359E2: main (main.cxx:32)

所有成员在编译时都是已知的,并且不再发生动态分配。 oraPatient.cxx:53中引用的行是构造函数。 sizeof(Patient)返回328,这正是分配为valgrind状态的大小。

在我的情况下,它总是这个类的最后两个成员。因此,如果我对其进行评论,则下一个将是QStringQDateTime。这就是为什么我认为它与Qt本身无关。

Patient.h

#define SimpleProperty(member, _type) \
  public: \
  /**
   * @brief Set member of type _type.
   * @param _arg_##member Value to be set
   * @see m_##member for a more detailed description
   */ \
  Q_INVOKABLE void Set##member(_type _arg_##member) \
  { \
    m_##member = _arg_##member;\
  } \
  /**
   * @brief Get member of type _type.
   * @return Returns the value of ##member
   * @see m_##member for a more detailed description
   */ \
  Q_INVOKABLE _type Get##member() const\
  { \
    return m_##member;\
  } \
  protected: \
    _type m_##member; \

class Patient : public RTObject
{
  Q_OBJECT

public:
  QString ProposeRootFolderName() const;
  explicit Patient(ora::Site *parent = NULL);
  virtual ~Patient();
  Patient(const ora::Patient & other);
  bool operator==(const Patient &patient);
  bool operator!=(const ora::Patient & patient);
  Q_INVOKABLE virtual QString GetStringRepresentation(const bool compriseChildren = true) const;
  Q_INVOKABLE virtual bool IsValid() const;
  Q_INVOKABLE void SetSite(ora::Site * parent);
  Q_INVOKABLE void SetParent(ora::Site * parent);
  Q_INVOKABLE ora::Site * GetParent() const;
  Q_INVOKABLE ora::Site * GetSite() const;
  Q_INVOKABLE bool Serialize(QDataStream &stream) const;
  Q_INVOKABLE void Deserialize(QDataStream &stream);
  Q_INVOKABLE const QList<ora::Treatment *> & GetTreatments() const;
  Q_INVOKABLE ora::Treatment * GetTreatment(const int treatmentIndex) const;
  Q_INVOKABLE bool AppendTreatment(ora::Treatment * treatment);
  Q_INVOKABLE bool InsertTreatment(ora::Treatment * treatment, const int index);
  Q_INVOKABLE bool RemoveTreatment(const int treatmentIndex);
  Q_INVOKABLE bool RemoveTreatment(ora::Treatment * treatment);
  Q_INVOKABLE void ClearTreatments();
  Q_INVOKABLE QList<ora::StructureSet *> & GetStructureSets();
  Q_INVOKABLE ora::StructureSet * GetStructureSet(const int structureSetIdx) const;
  Q_INVOKABLE bool AppendStructureSet(ora::StructureSet * structureSet);
  Q_INVOKABLE bool InsertStructureSet(ora::StructureSet * structureSet, const int index);
  Q_INVOKABLE bool RemoveStructureSet(const int structureSetIdx);
  Q_INVOKABLE int RemoveStructureSet(ora::StructureSet * structureSet);
  Q_INVOKABLE void ClearStructureSets();
  Q_INVOKABLE const QMap<QString, PatientSetup*> &GetPatientSetups() const;
  Q_INVOKABLE PatientSetup *GetPatientSetup(const QString &patientSetupID) const;
  Q_INVOKABLE bool InsertPatientSetup(const QString &patientSetupID, PatientSetup *patientSetup);
  Q_INVOKABLE bool RemovePatientSetup(const QString &patientSetupID);
  Q_INVOKABLE bool RemovePatientSetup(PatientSetup *patientSetup);
  Q_INVOKABLE void ClearPatientSetups();
  Q_INVOKABLE const QList<ImagingProtocol *> &GetImagingProtocols() const;
  Q_INVOKABLE const QList<TreatmentOperation *> &GetTreatmentOperations() const;
  Q_INVOKABLE const QList<Reg23Configuration *> &GetRegistrationConfigurations() const;
  Q_INVOKABLE ImagingProtocol *GetImagingProtocol(const int i) const;
  Q_INVOKABLE TreatmentOperation *GetTreatmentOperation(const int i);
  Q_INVOKABLE Reg23Configuration *GetRegistrationConfiguration(const int i);
  Q_INVOKABLE TreatmentOperation *GetTreatmentOperation(const QString toId);
  Q_INVOKABLE Reg23Configuration *GetRegistrationConfiguration(const QString rcId);
  Q_INVOKABLE bool AppendImagingProtocol(ImagingProtocol *protocol);
  Q_INVOKABLE bool AppendTreatmentOperation(TreatmentOperation *to);
  Q_INVOKABLE bool AppendRegistrationConfiguration(Reg23Configuration *rc);
  Q_INVOKABLE bool RemoveImagingProtocol(const int i);
  Q_INVOKABLE bool RemoveTreatmentOperation(const int i);
  Q_INVOKABLE bool RemoveRegistrationConfiguration(const int i);
  Q_INVOKABLE bool RemoveImagingProtocol(ImagingProtocol *protocol);
  Q_INVOKABLE bool RemoveTreatmentOperation(TreatmentOperation *to);
  Q_INVOKABLE bool RemoveRegistrationConfiguration(Reg23Configuration *rc);
  Q_INVOKABLE void ClearImagingProtocols();
  Q_INVOKABLE void ClearTreatmentOperations();
  Q_INVOKABLE void ClearRegistrationConfigurations();
  Q_INVOKABLE bool InsertImagingProtocol(ImagingProtocol *protocol, const int i);
  Q_INVOKABLE bool InsertTreatmentOperation(TreatmentOperation *to, const int i);
  Q_INVOKABLE bool InsertRegistrationConfiguration(Reg23Configuration *rc, const int i);
  Q_INVOKABLE const QList<Study *> &GetStudies() const;
  Q_INVOKABLE Study *GetStudy(const int i) const;
  Q_INVOKABLE Study *GetStudy(const QString sid) const;
  Q_INVOKABLE bool AppendStudy(Study *stud);
  Q_INVOKABLE bool RemoveStudy(const int i);
  Q_INVOKABLE bool RemoveStudy(Study *stud);
  Q_INVOKABLE void ClearStudies();
  Q_INVOKABLE bool InsertStudy(Study *stud, const int i);
protected:
  Patient &operator=(const Patient&);
  virtual void Initialize();
  ora::Site *m_Site;
  QList<ora::Treatment*> m_Treatments;
  QList<ora::StructureSet*> m_StructureSets;
  QMap<QString, PatientSetup*> m_PatientSetups;
  QList<ImagingProtocol*> m_ImagingProtocols;
  QList<TreatmentOperation *> m_TreatmentOperations;
  QList<Study*> m_Studies;
  QList<Reg23Configuration *> m_RegistrationConfigurations;
  SimpleProperty(FirstName, QString)
  SimpleProperty(LastName, QString)
  Q_PROPERTY(QString FirstName READ GetFirstName WRITE SetFirstName)
  Q_PROPERTY(QString LastName READ GetLastName WRITE SetLastName)
  SimpleProperty(PatientID, QString)
  Q_PROPERTY(QString PatientID READ GetPatientID WRITE SetPatientID)
  SimpleProperty(OtherPatientIDs, QString)
  Q_PROPERTY(QString OtherPatientIDs READ GetOtherPatientIDs WRITE SetOtherPatientIDs)
  SimpleProperty(BarCodeID, QString)
  Q_PROPERTY(QString BarCodeID READ GetBarCodeID WRITE SetBarCodeID)
  SimpleProperty(IssuerOfPatientID, QString)
  Q_PROPERTY(QString IssuerOfPatientID READ GetIssuerOfPatientID WRITE SetIssuerOfPatientID)
  SimpleProperty(BirthDate, QDate)
  Q_PROPERTY(QDate BirthDate READ GetBirthDate WRITE SetBirthDate)
  SimpleProperty(PatientSex, QString)
  Q_PROPERTY(QString PatientSex READ GetPatientSex WRITE SetPatientSex)
  SimpleProperty(BirthTime, QTime)
  Q_PROPERTY(QTime BirthTime READ GetBirthTime WRITE SetBirthTime)
  SimpleProperty(OtherPatientNames, QString)
  Q_PROPERTY(QString OtherPatientNames READ GetOtherPatientNames WRITE SetOtherPatientNames)
  SimpleProperty(EthnicGroup, QString)
  Q_PROPERTY(QString EthnicGroup READ GetEthnicGroup WRITE SetEthnicGroup)
  SimpleProperty(Comments, QString)
  Q_PROPERTY(QString Comments READ GetComments WRITE SetComments)
  SimpleProperty(PatientSpeciesDescription, QString)
  Q_PROPERTY(QString PatientSpeciesDescription READ GetPatientSpeciesDescription WRITE SetPatientSpeciesDescription)
  SimpleProperty(PatientSpeciesCodeSequence, QString)
  Q_PROPERTY(QString PatientSpeciesCodeSequence READ GetPatientSpeciesCodeSequence WRITE SetPatientSpeciesCodeSequence)
  SimpleProperty(ResponsiblePerson, QString)
  Q_PROPERTY(QString ResponsiblePerson READ GetResponsiblePerson WRITE SetResponsiblePerson)
  SimpleProperty(ResponsiblePersonRole, QString)
  Q_PROPERTY(QString ResponsiblePersonRole READ GetResponsiblePersonRole WRITE SetResponsiblePersonRole)
  SimpleProperty(ResponsibleOrganization, QString)
  Q_PROPERTY(QString ResponsibleOrganization READ GetResponsibleOrganization WRITE SetResponsibleOrganization)
  SimpleProperty(HospitalID, QString)
  Q_PROPERTY(QString HospitalID READ GetHospitalID WRITE SetHospitalID)
  SimpleProperty(PatientSize, qreal)
  Q_PROPERTY(qreal PatientSize READ GetPatientSize WRITE SetPatientSize)
  SimpleProperty(PatientWeight, qreal)
  Q_PROPERTY(qreal PatientWeight READ GetPatientWeight WRITE SetPatientWeight)
  SimpleProperty(Address, QStringList)
  Q_PROPERTY(QStringList Address READ GetAddress WRITE SetAddress)
  SimpleProperty(CountryOfResidence, QString)
  Q_PROPERTY(QString CountryOfResidence READ GetCountryOfResidence WRITE SetCountryOfResidence)
  SimpleProperty(TelephoneNumbers, QStringList)
  Q_PROPERTY(QStringList TelephoneNumbers READ GetTelephoneNumbers WRITE SetTelephoneNumbers)
  SimpleProperty(Station, QString)
  Q_PROPERTY(QString Station READ GetStation WRITE SetStation)
  SimpleProperty(BirthName, QString)
  Q_PROPERTY(QString BirthName READ GetBirthName WRITE SetBirthName)
  SimpleProperty(Occupation, QString)
  Q_PROPERTY(QString Occupation READ GetOccupation WRITE SetOccupation)
  SimpleProperty(SmokingStatus, QString)
  Q_PROPERTY(QString SmokingStatus READ GetSmokingStatus WRITE SetSmokingStatus)
  SimpleProperty(PregnancyStatus, QString)
  Q_PROPERTY(QString PregnancyStatus READ GetPregnancyStatus WRITE SetPregnancyStatus)
  SimpleProperty(Attendant, QString)
  Q_PROPERTY(QString Attendant READ GetAttendant WRITE SetAttendant)
  SimpleProperty(AttendanceDate, QDateTime)
  Q_PROPERTY(QDateTime AttendanceDate READ GetAttendanceDate WRITE SetAttendanceDate)
  SimpleProperty(AbsenceDate, QDateTime)
  Q_PROPERTY(QDateTime AbsenceDate READ GetAbsenceDate WRITE SetAbsenceDate)
  SimpleProperty(LastAttendanceDate, QDateTime)
  Q_PROPERTY(QDateTime LastAttendanceDate READ GetLastAttendanceDate WRITE SetLastAttendanceDate)

private:

};
QDataStream &operator<<(QDataStream &out, const ora::Patient &patient);
QDataStream &operator>>(QDataStream &in, ora::Patient &patient);

} // namespace ora

Q_DECLARE_METATYPE(ora::Patient)

Patient.cxx

#include "oraPatient.h"

namespace ora
{

Patient::Patient(ora::Site *parent) : RTObject(parent)
{
  Initialize();

  m_Site = parent;
}

bool Patient::operator==(const Patient &patient)
{
  return true;
}


bool Patient::operator!=(const ora::Patient &patient)
{
  return !(*this == patient);
}

} // namespace ora

RTObject.cxx

#include "oraRTObject.h"

// SORRY
#include "oraCentralLoggingService.h"

namespace ora
{

RTObject::RTObject(QObject *parent) : QObject(parent)
{
  qRegisterMetaTypeStreamOperators<ApprovalStatusType>("ora::RTObject::ApprovalStatusType");
  qRegisterMetaTypeStreamOperators<BeamTypeType>("ora::RTObject::BeamTypeType");
  qRegisterMetaTypeStreamOperators<PlanIntentType>("ora::RTObject::PlanIntentType");
  qRegisterMetaTypeStreamOperators<PlanRelationshipType>("ora::RTObject::PlanRelationshipType");
  qRegisterMetaTypeStreamOperators<PrimaryDosimeterUnitType>("ora::RTObject::PrimaryDosimeterUnitType");
  qRegisterMetaTypeStreamOperators<RadiationTypeType>("ora::RTObject::RadiationTypeType");
  qRegisterMetaTypeStreamOperators<RotationDirectionType>("ora::RTObject::RotationDirectionType");
  qRegisterMetaTypeStreamOperators<ScanModeType>("ora::RTObject::ScanModeType");
  qRegisterMetaTypeStreamOperators<TreatmentDeliveryTypeType>("ora::RTObject::TreatmentDeliveryTypeType");
  qRegisterMetaTypeStreamOperators<ModeType>("ora::RTObject::ModeType");
  qRegisterMetaTypeStreamOperators<RTObject::PatientPositionType>("ora::RTObject::PatientPositionType");
  qRegisterMetaTypeStreamOperators<RTObject::QStringStringMap>("ora::RTObject::QStringStringMap");

  m_StreamVersion = 0;
  m_IsValid = true;
}

RTObject::RTObject(const RTObject &)
{
  m_StreamVersion = 0;
  m_IsValid = false;
}

RTObject::~RTObject()
{
}

} // namespace ora

创建患者:new Patient();

1 个答案:

答案 0 :(得分:0)

这是我做的非常愚蠢的事情。在使用该库的应用程序中具有不同值的库中,必须简单地不使用#ifdef。 这导致我的应用程序头看起来与编译器不同,而不是库中的相同头,这当然会导致应用程序编译器计算错误的地址。