LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Graphing data on separate panel from main task has intermittent problems.

I have a panel with a Graph on it. I launch this panel from my main application when the user selects a button. When this panel is open I sometimes have intermittent problems. It seems to effect the communication with a device I have connected to the USB. It does not happen often but seems to be related to when I change the size of the array I am sending to the graph.  Specifically if I increase the size of the data sent to it, it has problems.
 
The array I send to this graph is dynamically allocated memory. After I send the data to the device I free the memory. Would this give the graph a problem? I figured after I populate the graph it must store this data in another memory location and does not need the original data array. Is this correct?
 
I am wondering if changing the size of the array being put on the graph might give it trouble. I don't think so because I have other graphs that I do the same thing to but they do not seem to have any trouble. 
 
Here is where I launch the panel:
 
 
int CVICALLBACK DisplayMagnitudeGraph (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   if ((MagPanelHandle = LoadPanel (0, "mag_reader.uir", MAGNITUDE)) < 0)
    return -1;
   g_MagGraphActive = 1;
   SetCtrlAttribute(panelHandle,PANEL_DISPLAY_MAGNITUDE,ATTR_DIMMED,1);
   DisplayPanel (MagPanelHandle);
   RunUserInterface ();
   DiscardPanel (MagPanelHandle);
   break;
  case EVENT_RIGHT_CLICK:
   break;
 }
 return 0;
}
 
Here is where I populate the graph:
 
   /*  update the Magnitude display */
   if( 1 == g_MagGraphActive )
   {
    /* Set the x-axis of the magnitude plot to display correct time  */
    SetCtrlAttribute(MagPanelHandle,MAGNITUDE_MAG_GRAPH,ATTR_XAXIS_GAIN, 10000.0/g_iSampleRateFreq);
   
    
    /* clear Magnitude graph  */
    DeleteGraphPlot(MagPanelHandle,MAGNITUDE_MAG_GRAPH, -1, VAL_DELAYED_DRAW);
   
   
   
    plotHandle = PlotY (MagPanelHandle, MAGNITUDE_MAG_GRAPH, Magnitude_MagData, g_iDataLength, VAL_DOUBLE,
      VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
    strcpy( legendText, "Magnitude");
    SetPlotAttribute(MagPanelHandle, MAGNITUDE_MAG_GRAPH, plotHandle,ATTR_PLOT_LG_TEXT, legendText);
    for( i=0; i<g_iDataLength; i++)
    {
     Magnitude_MagData[i] = Average;
    }
    plotHandle = PlotY (MagPanelHandle, MAGNITUDE_MAG_GRAPH, Magnitude_MagData, g_iDataLength, VAL_DOUBLE,
      VAL_FAT_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_GREEN);
    strcpy( legendText, "Average");
    SetPlotAttribute(MagPanelHandle, MAGNITUDE_MAG_GRAPH, plotHandle,ATTR_PLOT_LG_TEXT, legendText);
   }
 
I am certain I am allocating enough memory for Magnitude_MagData so I figure the problem must be having the extra thread (MagPanelHandle) getting focus and not letting  main application do its work.  Any ideas or resources to look at to better understand what I am doing wrong. I am a novice LabWindows guy so I could be doing something that looks obviously wrong to a more experienced person.
 
thanks,
Don Pearce
 
 
 
 
0 Kudos
Message 1 of 3
(3,681 Views)
Hi Don,

Based on the code you posted, I can't tell if your application is truly multi-threaded. Simply loading a new panel and calling RunUserInterface does not spawn a new thread, and you shouldn't be doing this anyway (calling RunUserInterface more than once). Please refer to the CVI help section titled "Different Approaches to Multithreaded User Interface Programming" for the proper way to create a dialog in a new thread. There are many considerations that need to be addressed with creating multi-threaded applications, and this is especially true when you are dealing with user interface components in CVI.

In regards to the issue you are seeing, it is too hard to tell what is causing the problem before you address the above issue. I wrote a simple program that dynamically allocates an integer array, plots it to a graph, and then frees the memory. The graph handled it just fine which implies that it does indeed have its own memory buffer behind the scenes. This fact leads me to believe that the strange behavior is more of a function calling RunUserInterface twice.

It may help you to look at the multi-threaded user interface example that ships with CVI (as a starting point):
CVI\samples\utility\Threading\ThreadPool\MultiPanel

Regards,
0 Kudos
Message 2 of 3
(3,645 Views)
Hello Don,

On the narrow question of whether it's safe to discard the plot data after you call the plotting function, that depends on whether you have the "Copy Original Plot Data" option enabled in the Edit Graph panel of the UI editor. If you do, then you're free to discard or overwrite the memory. If you don't (this is a memory optimization) then the graph control will attempt to read that memory if, for example, the graph scales change. You can find out more by right-clicking on the graph in the UI editor, selecting "Control Help" and then click on the "Programming Graph Controls" link.

Luis
0 Kudos
Message 3 of 3
(3,642 Views)