1

I found information from this thread and am starting to wonder if the information provided there is relevant to the cause of the issue I'm facing.

The QMap takes the following template arguments:

QMap< QString, int >

My main issue is that I'm initializing a cube class, and within that class contains various calls which are supposed to do very specific things.

One in particular revolves around setting up the class' QMap members.

Observe the following:

mGeometry = new Geometry;
mGeometry->Init();

mCamera = QVector3D( 10.0, 10.0, 10.0 );

mCubeage.insert( "cubes", 0 );

Debug Output from mCubeage.insert( "cubes", 0 );

// ASCII compatibility

1) (uh oh)

#ifndef QT_NO_CAST_FROM_ASCII
    inline QT_ASCII_CAST_WARN_CONSTRUCTOR QString(const char *ch) : d(fromAscii_helper(ch))
    {}

2)

template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey,
                                                                       const T &avalue)
{
    detach(); //the called function

    QMapData::Node *update[QMapData::LastLevel + 1];
    QMapData::Node *node = mutableFindNode(update, akey);
    if (node == e) {
        node = node_create(d, update, akey, avalue);
    } else {
        concrete(node)->value = avalue;
    }
    return iterator(node);
}

3)

bool operator==(const QMap<Key, T> &other) const;
    inline bool operator!=(const QMap<Key, T> &other) const { return !(*this == other); }

    inline int size() const { return d->size; }

    inline bool isEmpty() const { return d->size == 0; }

    inline void detach() { if (d->ref != 1) detach_helper(); } //the called function/if statement
    inline bool isDetached() const { return d->ref == 1; }
    inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
    inline bool isSharedWith(const QMap<Key, T> &other) const { return d == other.d; }
    inline void setInsertInOrder(bool ordered) { d->insertInOrder = ordered; }

inline bool operator!=(int value) const
    {
        return _q_value != value;
    }

4)

inline bool operator!=(int value) const
    {
        return _q_value != value;
    }

And...that's where it stops - BOOM, segmentation fault. According to some of the Qt forums out there - in the realm of Internet - it is unwise to declare pointers to QMaps, which I originally thought of doing mainly because of the fact that one possible reason for this happening could be due to the fact that the QMap may not have been initialized properly. Yet, the fact that it's a class member which has a default constructor I believe contradicts this.

Question

According to the above, why is this happening? What can be done about it?

Thoughts

Before someone mentions the fact that I'm passing a const char* to a QString parameter, I should note that I have already tried declaring a QString and passing it that as an argument. I should also probably note that I'm running this from Ubuntu 11.10, in Qt Creator.

3
  • 1
    it is unwise to declare pointers to QMap - don't beleive everything you see on intenet Jan 20, 2012 at 5:32
  • How did you declare mCubeage; as a QMap<QString, int> ?
    – Neox
    Jan 20, 2012 at 6:40
  • Yup. Although I've switched that to a pointer to a QMap now, mainly due to the fact that I got a heap allocation warning via Valgrind. According to James Mcneallis (if that's how you spell his name), you don't need to initialize objects allocated on the stack (if they're class members), so, I didn't initialize them. But, Valgrind gave me warnings about this, so I just decided to make a bunch of my class members pointers, and therefore have been working on the memory management since.
    – zeboidlund
    Jan 20, 2012 at 19:54

1 Answer 1

2

What can be done about it?

There is only one thing : run your program under valgrind, and fix all problems you find.

Why is it happening?!? Who knows? You didn't provide enough information. Maybe stack smash, maybe you overwrote passed the end of some arrays, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.