Thread: queue, threads and pointers

  1. #1
    Registered User
    Join Date
    Jun 2004

    queue, threads and pointers

    I have some code here that crashes (AV) sometimes and I can't figure out why, although there are some strange things in there.

    There's an std::queue object that's tested for empty first.
    If it is empty the application will wait 10milliseconds for a message to arrive and if not it returns. If there was a message or there is a message now the first one is taken from the queue, processed and deleted.
    Here's the code:

    if (Messages.size() == 0)
    if ( ! MessageArrived() )

    Message* msg = Messages.front();


    delete msg;

    The code crashes on Messages.front();
    Now I was thinking if the deletion of msg goes wrong because it happens before the pop() or because this code is threaded that something goes wrong.

    Anybody has some theories about this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    That really depends on how you are pushing messages onto your queue in the first place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>delete msg;
    What exactly are you deleting?
    >> Messages.pop();
    Is this safe after the delete?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 33
    Last Post: 05-14-2009, 10:15 AM
  2. Replies: 2
    Last Post: 11-03-2008, 04:31 PM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. threads...
    By newbie_grg in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2003, 07:47 AM
  5. My First Thread (functinal pointers)
    By Denis Itchy in forum C Programming
    Replies: 13
    Last Post: 09-02-2002, 11:14 PM