2015-07-02 74 views
1

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

你可以在這裏粘貼代碼valgrind嗎? – Cheiron

+3

「爲什麼我的代碼不會顯示給您,它有內存問題?」詢問誰可以*看到你的代碼。 – Angew

+0

添加代碼。缺少的東西有希望不相關,否則我可以發佈它們。 – user1760653

回答

0

這是相當愚蠢的事我沒有。在使用該庫的應用程序中,不必使用#ifdef庫中的值不相同。 這導致我的應用程序頭看起來不同於編譯器,而不是庫中的相同頭,這當然會導致應用程序編譯器計算錯誤的地址。