Question

hi i'm trying to send a QList as a parameter to another class but for some reason i got a read access violation...

CompareTimeChannel.h

 class CompareTimeChannel : public IDataChannel
   public:

      // @brief The method used to receive the list
      void setData(const QList< QSharedPointer<core::ITrackSection> > & sections);

      // @brief The list
      QList< QSharedPointer<core::ITrackSection> > _sections;

 };

CompareTimeChannel.cpp

 // @brief Empty constructor
 CompareTimeChannel::CompareTimeChannel()
 {
 }

 void CompareTimeChannel::setData(const QList< QSharedPointer<core::ITrackSection> > & sections)
 {
     //_sections = *new QList< QSharedPointer<core::ITrackSection> > ();
     _sections.clear();
     _sections.append(sections);
 }

Running this code will throw Exception at 0x31cc78d, code: 0xc0000005: read access violation at: 0x4, flags=0x0 on _sections.clear();

I tried to initialize the list before (the commented line _sections = *new QList<...>) but the exception is thrown the same.

An answer would be very appreciated...

EDIT

Ok it's fixed!

First, like @AndreasT said, i had to initialize the default QList constructor.

Then, according to @10WaRRioR01 's answer, the issue comes from CompareTimeChannel which wasn't initialized the first time the method was called. Fixed using :

        CompareTimeChannel* chan = static_cast<CompareTimeChannel*>(channel);
        Q_ASSERT(chan);
        if (chan) {
            chan->setData(sections);
        }
        else {
            qDebug() << "Dynamic cast failure";
        }

Thank you all, guys!

Était-ce utile?

La solution

//_sections = *new QList< QSharedPointer<core::ITrackSection> > ();

You shouldn't ever do something like this. This creates a new instance of QList on the heap and it will never be deleted, so you have a memory leak
You should have done

_sections = QList< QSharedPointer<core::ITrackSection> > ();

instead, and it would be legal. But the most simple way is to use a copy assignment like this

_sections = sections

The problem you got is most likely related to the data you have in _sections. Maybe you are calling your methods on a null CompareTimeChannel object

Autres conseils

You should initialize sections in the constructor. The commented line is just horribly wrong.

new constructs the List on the heap, then you dereference that with *new and the assignment implicitly calls the copy constructor of the new list on the Heap and copies that into the instance. The thing on the heap is still aruond though, so you just created a memory leak.

// @brief Empty constructor
 CompareTimeChannel::CompareTimeChannel()
:_sections()   // initialization default constructor.
 {
 }

Edit regarding the comment:

The QList.clear() method calls the destructors of every element of the list. At least one of your shared pointers seems not to be initialized correctly. If you need more info, please paste the code that puts stuff into _sections.

Edit Regarding the exception: As I said the problem is most likely with the shared pointers not being set to anything interesting. When the SP gets destroyed it calls the destructor of its content, which must exist, otherwise it throws a read access violation, which would explain the symptoms.

This what you showed should work. Your problem is in some other place.
This kind of problems might be caused by many different mistakes, like: bad static_cast or bad c-style cast, break in binary compatibility when you are using dynamic libraries, write outside of table, problems with compiler cache (this happens is quite often so cure for that is below).

First what I would try to do:

make clean
qmake
make

This fixes such problem quite often. if i doesn't help you have to find other problems in your code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top