LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

thread safe queue - memory leak

Hi,

In my application I'm using tsq for collecting incoming instructions from other device (user), proccessing them, and then replying according to each instruction. Program has one thread for receiving, and another thread for processing instruction. Application doesn't know when and how many instructions is coming, so every instruction is read and proccessed immediately (one element written in tsq, and one element read from tsq). Type of data written in tsq is structure consists of few chars, ints etc. of size approx. 50 Kb.

My problem is that every time writer thread puts instruction in tsq, memory usage increases for size of that element, but when other thread reads the data, memory usage remains the same, causing memory leakage. Since my application must run continously for a long time, it is a problem because after some time (depends on number of incomming instructions) it crashes.


This is receiving of data and puting it to tsq:

static void CVICALLBACK Receiveinstruction (void *data)
{
    typedef struct KomData
    {
        char                comm_id[100];
        unsigned int        status;
        int                type;
        char                user[256];
        unsigned int        handle;
        unsigned int        priority;
        char                command[50000];
        unsigned int        threadId;
        unsigned int        threadId_2;
        char                *result_ptr;
    } KomData;

    KomData            komData;
    KomData            *komDataPtr = &komData;

    /*
    .... part which receive data from user....
    */

    CmtWriteTSQData (tsqHandle, komDataPtr, 1, TSQ_INFINITE_TIMEOUT, NULL);                                

    /*
    .... other ....
    */
}      

This is reading from tsq:

void CVICALLBACK ReadTSQ (int queueHandle, unsigned int event, int value, void *data)
{

    KomData    komData;
    KomData    *komDataPtr = &komData;


    /*
    .......
    */


    numItemsRead = CmtReadTSQData (queueHandle, &komData, numItemsRead, TSQ_INFINITE_TIMEOUT, 0);
          
    komDataPtr = &komData;

    /*
    .......
    */

}


Thanks for any suggestions.

Pero
0 Kudos
Message 1 of 2
(3,303 Views)
This is odd functionality because when you perform a read on the queue, it reads the item(s) and they are removed from the queue, which is pretty much the reason for the queue. There are a few things we need to do.

1) Create an indicator and have it receive information from CmtGetTSQAttribute with ATTR_TSQ_ITEMS_IN_QUEUE as the attribute. You can set this to grab the number of items on time based or when you press a button, just as long as we can verify that the queue is in fact continuously growing.

2) Are you reading at a much slower rate than you are writing? If so this could cause problems with memory leakage.

3) Are you creating the TSQ in a loop? If so this would cause multiple queues to continue being made and grow pretty fast.


Also, please check out the following link for more information on multithreading in CVI and TSQ

http://zone.ni.com/devzone/conceptd.nsf/webmain/2DF76D8073AA2A8C86256D2C005586D2?opendocument

Brandon Vasquez
Applications Engineer
National Instruments

Message Edited by Brandon V on 08-25-2006 05:08 PM


Brandon Vasquez | Software Engineer | Integration Services | National Instruments
0 Kudos
Message 2 of 2
(3,272 Views)