LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic memory is corrupt (Labwindows/cvi 2010)

I have a multi threaded application....where I have a buffers allocated ineternally in the threads,,...A user interface is present to abort the therad execution.

 

Here is the code in one of the threads (NOTE: status is set by a user button to EXIT the program):

 

int EthernetDataStructureThread (void *functionData)
{
    CmtSetCurrentThreadPriority (2);
   
    EthernetData GetData={0}, *data=NULL;
    data = &GetData;
   
    /* Use a buffer to read the data from the queue which is coming from TCP read with fresh data */
    const void *BufferEth;
   
   
    int BytesRead=0;
    int BytesWritten=0;
    char ReadErrorMessage[256];
    BOOL bitset = FALSE;
    if ( (BufferEth = (EthernetData*)malloc(EthernetDataSize)) != NULL)
    {
        while ( !status )
        {
       
            BytesRead = CmtReadTSQData (Queue_TCPRead_EthernetData, BufferEth, EthernetDataSize, TSQ_INFINITE_TIMEOUT, 0);
            memcpy (data, BufferEth, EthernetDataSize);
            /* Empty the queue so that we don't have data in next time. */
            CmtFlushTSQ (Queue_TCPRead_EthernetData, TSQ_FLUSH_ALL, NULL);
       
            //Pass on the data to the Main thread
            BytesWritten = CmtWriteTSQData (Queue_EthernetData_Main, BufferEth , EthernetDataSize, 1, NULL);
            if ( BytesWritten != EthernetDataSize)
            {
                 free(BufferEth);
                BufferEth = NULL;
                exit(0);
            }
            Delay(0.02);
        }
        if (status && !bitset)
        {
            free(BufferEth);
            BufferEth = NULL;
            bitset = TRUE;
        }
    }

    return 0;
}

 

 

My problem is: free(BuffEth) always says Dynamic Memory is corrupt......When I put a breakpoint and keep seeing the resource tracking window, I can see it getting executed fine as the resource tracking window greys out the memory block....but then suddenly free wants to exceute again...and causes the "dynamic memory is corrupt" error.

 

I am using LABWINDOWS /CVI 2010 SP1 in WinXP.

 

Please help me ASAP....very much appreciated.

 

 

Regards

NITIN

-Nharish
0 Kudos
Message 1 of 19
(5,809 Views)

Anybody from NI watching this thread,....PL HELP...SOS

-Nharish
0 Kudos
Message 2 of 19
(5,789 Views)

Hi Nharish,

 

According to your program, the free(BufferEth) executes twice because you call it twice. If you believe this is executing twice on the same step, add a printf function with a unique tag after the first instance of the free(BufferEth) and again after the second instance. When you run the program you should see the first and second printf instance only once. If you see these twice each, then you are correct, the free(BuffereEth) function seems to run twice and this is probably not correct.

 

Regarding the "dynamic memory is corrupt" error, please consult the following KnowledgeBase for common reasons and solutions for eliminating this error in LabWindows/CVI. Please go through these troubleshooting steps and post back if you have further questions. Good luck with your application and have a great day!

 

Regards,

Jackie

DAQ Product Marketing Engineer
National Instruments
0 Kudos
Message 3 of 19
(5,767 Views)

Thank you for your reply Jackie.

 

I am calling Free only once...there is an If condition there.

 

See the attched video for my problem...Free frees the memory block (see the greyed out portion in resource tracking window but still the statement controls remains there and free executes again, throwing error).

 

 

Do you  think it is some sort of bug in LABWINDOWS.

 

See the attached video.

 

 

-Nharish
0 Kudos
Message 4 of 19
(5,760 Views)

Hi Nharish,

 

I did view your video, but I do not see where the printf functions execute. Do you see the "before call" and "after call" string? How many times do you see these? Have you gone through any of the troubleshooting steps in the KnowledgeBase article and if so, what were the results? There are many explanations for why this error occurs and troubleshooting steps to remedy this error in the article and I am hesitant to assume it is a bug in LabWindows before we have exhausted all our resources in order to try to fix this. Please let me know what troubleshooting steps you have pursued out of the article and what the results were.

Regards,

Jackie

DAQ Product Marketing Engineer
National Instruments
0 Kudos
Message 5 of 19
(5,733 Views)

I only see the first printf ("before call") and there is no output for the second one...because code execution stops at the free statement.

 

As far as the article is concerned....I did not find any relevant conditions for my case...I am just simply allocating memory using malloc and then setting some values, after that I am using free where the error pops up....

 

It is just these 3 statements only. Rest all is in if case which never becomes true.

 

 

 

 

-Nharish
0 Kudos
Message 6 of 19
(5,729 Views)

Hi Nharish,

 

Could you please attach a simple CVI application that demonstrates this error so that I can run this on my end to see if I can reproduce this?

 

Thanks,

Jackie

DAQ Product Marketing Engineer
National Instruments
0 Kudos
Message 7 of 19
(5,713 Views)

Can you please give me some private location...I dont want to share my code in public domain.

 

 

-Nharish
0 Kudos
Message 8 of 19
(5,711 Views)

Hi Nharish,

 

You can upload this to our FTP site by opening up a Windows Explorer window and navigating to ftp://ftp.ni.com/incoming. You should see a bunch of files. Please copy your application onto the site and name it "dynamic error application" so I can easily find it.


Regards,
Jackie

DAQ Product Marketing Engineer
National Instruments
0 Kudos
Message 9 of 19
(5,696 Views)

Here you go:

 

ftp://ftp.ni.com/incoming/dynamic error application.zip

 

Run the code....you will see a GUI.....it will have just 1 switch....click it after 5 secinds.....you will see the error and error location.

 

Let me know...If you need somthing else from my side.

 

Thank you so mcuh for looking into this issue.

 

 

-Nharish
0 Kudos
Message 10 of 19
(5,694 Views)