views:

134

answers:

2

I have in an Object an QVector of Coordinates (my type) that i want to transfer to an other Vector ( i validate and than want to use ist ).

Header

bool getVector(QVector<Coordinates> &getCoordinates );

C File

static QVector<Coordinates> current;


int getVector( QVector<Coordinates> &getCoordinates)
{

.... stuff ...

 getCoordinates = current;

.... stuff ....
return 0;
}

And i use it like

....

QVector<Coordinates> currentCoordinates;
getVector(currentCoordinates);

currentCoordinates.X // CRASH

the debugger goes to this line where an Live Crah happens

  inline QVector(const QVector<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }

so my how can i fix this? As i can use this to get all the other Variables with this methode.

Thanks Elektro

A: 

A likely cause of your problem is that current has not been constructed before getVector is called. Initialization of static objects in C++ is a thorny area, and a frequent source of bugs - for more information, see this question, and the static initialization order fiasco FAQ entry.

A simple solution to this problem is to provide access to current via a function, i.e. replace

static QVector<Coordinates> current;

with

static QVector<Coordinates>& getCurrent()
{
    static QVector<Coordinates> current;
    return current;
}

Note, however, that the function as written above is not thread-safe. If multiple threads may call getCurrent, then it should be protected with a QMutex.

Gareth Stockwell
I did what you said. It worked just fine.But than i was told, that if i just change QVector<Coordinates> to an own Type like typdef QVector<Coordinate> VesselCoordinates, that i should work. And it work just fine as well. Even as i was deleting your Function it still worked just fine.thank you
Thomas
@Thomas, just using a typedef should not change the behaviour of the program at all - it is just 'syntatic sugar' for the compiler. Are you sure that just adding a typedef to your original program fixes the crash?
Gareth Stockwell
yes just the typedef fixed it. As i was told it makes that the compiler resolve it just in time and than the Vector is been constructed. We trick the compiler.
Thomas
A: 

For gareth and the Forum :

the header:

typedef  QVector<Coordinates> VesselCoordinates;

  bool (*getVessel)(Charakter forCharakter, Vessel& getVessel,VesselCoordinates &getCoordinates );

later i bind tis function pointer to an static function ( cause this part of my Program will be one day convertet to c)

cpp file lower layer:

     static struct {
            Charakter currentPlayerVessel;
            VesselCoordinates possibility;
        }data;
         static bool getVessel(Charakter forCharakter, Vessel& getVessel,VesselCoordinates &getCoordinates );

// funktion to bind the funktion pointer to this static funktion so it can be called outside the File

        static bool serverNamespace::getVessel(Charakter forCharakter, Vessel& getVessel,VesselCoordinates &getCoordinates )
        {
            bool retValue= false;

            if ( forCharakter == data.currentPlayerVessel){
                // TODO abfragen ob die Adresse regestriert ist!
                if ((true == minSize()) and  ((true == shipsInRow())or (true == shipsInLine())))
            {
                retValue = true;
                Vessel test = (Vessel)data.possibility.size();
                getVessel =  test;
                getCoordinates = data.possibility;
            }
        }
        return retValue;
    }

And then i can use this in the upper layer cpp file to get the information i need:

// in an Funktion : 

 VesselCoordinates currentCoordinates;
    currentCoordinates.clear();

    Vessel currentVessel;

if (true == basicFleet->getVessel(currentCharakter,currentVessel, currentCoordinates ))

// doing stuff to it 

so its worik fine but your idea worked just as fine. Maybe you can see why my idea is also working.

Thank you

elektor

Thomas