Server Help

Non-Subspace Related Coding - [C++] Stupid bug... [SOLVED]

Samapico - Sat Oct 24, 2009 4:10 pm
Post subject: [C++] Stupid bug... [SOLVED]
We're making this program for school, it has to simulate a TCP/IP protocol using UDP. Basically a couple of networking layers, each has its own thread, two queues for requests (from layer above) and indications (from layer under).
We're having this very weird bug, the queue works fine, but at some point, I get memory access violation error... It appears that the queue object in the layer is... moved?! When I put a watch on 'this' while in the queue, it's like 0x00000008 or something like that, which is nowhere near a good pointer.
I used the linkedlist from mervbot core, but I also had the issue with some STL queue I tried.

I can't figure out the problem... I'm hoping some of you guys could help me out new_let_it_all_out.gif
The source has a bunch of files and stuff, but I'm sure you'll find a way through it all... The server project works fine, but the client one gives the exception after sending a request to a layer.

Have fun :/ and thank you in advance


<attachment removed>
Doc Flabby - Sun Oct 25, 2009 8:36 am
Post subject:
For some reason queue.cpp is a 0 byte file in that rar you uploaded icon_sad.gif
Samapico - Sun Oct 25, 2009 1:22 pm
Post subject:
hmm, it's not even part of the project, the _queue type I used is in types.h/cpp, but I think when I uploaded it it's actually using a _linkedlist , also in types.h/cpp

Attached proper arborescence of both projects in case you're not using visual studio; a couple of files in the .rar are not used
Samapico - Sun Oct 25, 2009 5:33 pm
Post subject:
i was able to go one more step forward, somehow...

I was able to send a message from the client to the server, the server network layer received it, but then I get the same crash...

Worse part is that i have no idea what I did to fix it... I moved some stuff around, but none of these things should change anything >=(

I guess I'll check the diffs through SVN and test a bunch of stuff...
Doc Flabby - Sun Oct 25, 2009 8:28 pm
Post subject:
I've found your problem samp.

The linked list you are using isn't threadsafe. I think this might be the problem.

So when two threads enter the "append" method at the same time, they interact in an unpredictable way. If you step into it, and look at the other thread in visual studio you'll see they are both in the same method.

http://img193.imageshack.us/img193/9527/forsamp.jpg

You need to use a threadsafe class or make it thread safe, using mutex's or something similar. If two threads interact like this, the result is unpredictable, which is why sometimes it works and sometimes not.
Samapico - Sun Oct 25, 2009 9:57 pm
Post subject:
But... aren't these 2 different instances of the list???
Doc Flabby - Mon Oct 26, 2009 6:45 am
Post subject:
Hmm, if there two different instances they shouldn't interact... I'll take another look at it when i'm more awake, it was just after midnight on a sunday -_-
Samapico - Mon Oct 26, 2009 1:23 pm
Post subject:
I also tried using critical sections while anything is appended or removed from the list, but that didn't work (you can still see these sections commented out)
Doc Flabby - Mon Oct 26, 2009 2:55 pm
Post subject:
I had another look and got no closer, i tried the critical section thing too...The thing that makes me think its a threading issue is because if i put a breakpoint on the "Append" Method and step though it slowly, i dont get the error, but if i run it at full speed it crashes.
Samapico - Mon Oct 26, 2009 3:07 pm
Post subject:
it doesnt crash on the first append... break at the 2nd .addrequest in c_main.cpp, and go slow, it should crash
Doc Flabby - Mon Oct 26, 2009 3:57 pm
Post subject:
Found it icon_smile.gif You were right it had nothing to do with threading.

Your trying to call a method on a NULL pointer. Line 44 in layer_transport.cpp.

Reason why it was so hard to find is because of this which hides the problem...
Code: Show/Hide

   Layer* Session()   { return layer_above; }
   Layer* Network()   { return layer_below; }


you need to provide the layer_below with a valid pointer ^^

Code: Show/Hide

void Layer_transport::RequestRcvd(Request *req)
{
   HeaderTransport h;
   char* iframe = NULL;


   
   switch (req->type)
   {
   case REQ_SendData:
      h.flags = DATA;
      h.length = req->frame->length;

      req->frame->Prepend(&h, sizeof(h));
      
      if(Network()==NULL) { printf("Null pointer"); } //i added this

      
      Network()->AddRequest(REQ_SendData, req->targetid, new DataBuffer(req->frame), NULL);  //calling a NULL pointer
      //P_DataReq(req->data, req->datalen);
      break;

   case REQ_ConnectRequest:
      break;
   case REQ_Disconnect:
      break;
   default:
      break;
   }
}

Samapico - Mon Oct 26, 2009 4:34 pm
Post subject:
F*************************** &*"/$6Y&*"$?&*

I FORGOT TO USE THE ATTACHLAYERS GODDAMNIT

It makes sense now... THANK YOU

I remember thinking that I should check if I did attach the layers, but I was probably sleeping or something, and I completely forgot...
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group