The problem is at line 12 loginRequest is created on the stack, that means that it is automatically has the lifetime of the code block it is in which is the if statement.
You take pointers to it and put them on your queue but that doesn't change the fact that the original object is deleted when its lifetime expires.
You need an object whos lifetime is not limited to the code block it is created in. That would be an object allocated from the heap using new.
Change how you use your queue so that the pointers it contains always point to objects allocated using new. When you remove items from the queue remember to delete them once you are finished with them. Possibly look into using std::auto_ptr or a smart pointer or handle class.
Is there any reason you didn't use a std::queue? Is QQueue at QT class?
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
Actually this piece of code is not really interested in the sub-class at all. It just needs a Request* in order to queue the request.
It looks like you could use a factory method. A factory method is often a static member of a base class that accepts data and uses the data to create a sub-class of that base but returns a base class pointer.
This keeps the code the knows how and when to create the sub-classes nicely encapsulated in the class your code you then look something like
Request *request=Request::requestFactory(line);
if (request != NULL)
{
requests.enqueue(request);
}
The factory method looks something like
Request::requestFactory(const QSTring& line)
{
RequestTypeType requestType = <Decode type From Line>;
Request* retval = NULL;
switch(requestType)
{
case REQUEST_LOGIN:
request= new LoginRequest;
request->tcpMessage=line.toUtf8();
request->decodeFromTcpMessage();
break;
case OTHER_REQUEST_TYPE:
request= new OtherRequestType;
request->tcpMessage=line.toUtf8();
request->decodeFromTcpMessage();
break;
...
}
return request;
}
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92