PDA

View Full Version : Segmentation error at a simple return this->property ?



Daimonie
18th February 2009, 00:02
Hey guys,
my program quits with a segmentation fault.

The gnu debugger tells me its these lines:


TorpiaDownload * download = this->downloads.at (processID);
QString sPath = download->path ();


QString TorpiaDownload::path ()
{
return this->sPath;
}



#ifndef TORPIADOWNLOAD_H
#define TORPIADOWNLOAD_H
#include <QString>
class TorpiaDownload
{
public:

QString path ();
int id ();

void setPath (QString path);
void setProcess (int ID);
private:
QString sPath;
int processID;
};

#endif


edit: after reading a bit..

int HTTPObject::downloadFile ( QString paramPath)
{
int processID = this->downloads.size ();

if (processID >= this->maxRequests)
{
return -1;
}
TorpiaDownload currentDownload;
currentDownload.setPath (paramPath);
currentDownload.setProcess (processID);

this->downloads.append (&currentDownload);
this->downloadQueue.append (processID);
if (this->timer->isActive () == false) {
timer->start (this->iInterval);
}
return processID;
}



now, what i'm trying to do here isn't that sophisticated.

The TorpiaDownload object holds a path and an id. Now, i want to ask for those two little things. I don't want 'you' to be able to edit them; no, you must use setPath for that.

Basically, i wanted QString path () to be of type const. But, that didn't work. So i removed it, and i still got that segmentation error .

Anybody sees my mistake?

Ps: i'm not used to working with the const keyword. any examples on how to return these (IE, have a normal property and a function that returns it, but casted to const), would be very useful :)

thanks in advance

Daimonie
18th February 2009, 00:32
After reading up a bit, i found out what a segmentation error is.
I made the TorpiaDownloader::sPath public, which fixed the problem a bit.

I still need your help on how i return a const value properly.

wysota
18th February 2009, 00:44
I'm pretty sure this line is incorrect:

this->downloads.append (&currentDownload);
You are storing a pointer to an object that is created on stack and will be deleted once you leave the scope. I'd say the pointer returned by the first line of the first snippet of code you pasted is invalid. Based on how your TorpiaDownload object looks like I suggest to store the object itself and not a pointer to it in the list. All your problems should go away.

Daimonie
18th February 2009, 00:51
I'm pretty sure this line is incorrect:

this->downloads.append (&currentDownload);
You are storing a pointer to an object that is created on stack and will be deleted once you leave the scope. I'd say the pointer returned by the first line of the first snippet of code you pasted is invalid. Based on how your TorpiaDownload object looks like I suggest to store the object itself and not a pointer to it in the list. All your problems should go away.
Yup, you're right .That's the thing i figured after editing my post and reading up more about Segmentation arrows.

Now, could by any chance somebody show me how to properly return a constant?


#ifndef TORPIADOWNLOAD_H
#define TORPIADOWNLOAD_H
#include <QString>
class TorpiaDownload
{
public:

QString path () const;
int id () const;

void setPath (QString path);
void setProcess (int ID);
private:
QString sPath;
int processID;
};

#endif

Now, i *think* this is the right way

int TorpiaDownload::id () const {
return this->processID;
}

But it gave me errors, if i recall correctly. Am i wrong, or is this not the right way and
how should i do it? :p

wysota
18th February 2009, 00:55
What exactly is the problem? What error message do you get? The code above looks fine.

By the way, I don't see how making a variable public can make a segmentation fault go away. And I don't consider it related to what I said previously.

talk2amulya
18th February 2009, 07:22
int TorpiaDownload::id () const {
return this->processID;
}

although it seems all correct, can u just try using "return processID", instead of "return this->processID" .. i believe 'this' pointer would be used by compiler anyway, but just to play around....

kleag
18th February 2009, 11:41
Hello,

I also think that "this>" are nearly never usefule.
I also agree that const has nothing to do with the segfaults. It can just make easier to understand and thus debug your program.

Concerning the path QString member, you should always give a reference or a pointer to the setter and take a reference from the getter. As you did, you use copies of the object each time.

You can then use const references:



const QString& path () const {return sPath;}
void setPath (const QString& path) {sPath = path}


Gaël