From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

General protection fault when using ListGetDataPtr with SetTableCellRangeVals for strings

Hi,

I'm using LabWindows CVI V5.5 on Windows 98 to read data from a file
into a table. I use the List functions from the programmer's toolbox
to first store all the file data in memory before filling an
appropriate range of cells using the SetTableCellRangeVals function,
because this is much faster than using SetTableCellVal with individual
values from large files. Everything works fine for doubles, but I'm
having problems with strings. If I run the following test code, I get
a repetition of one element of the string array in column 5 as
expected:

ListType CellStrings = ListCreate(sizeof(char) * MAX_STRING_LEN);
....
// code to gather strings from file using ListInsertItem here - this
works
....
// (table already formatted to accept strings)
SetTableCellVal (data_p, DATA_PANEL_TABLE, MakePoint(5, FirstDataRow),
ListGetPtrToItem (CellStrings, 1)); // this works

//The following should fill a range of cells, but I get a general
//protection fault instead...
SetTableCellRangeVals (data_p, DATA_PANEL_TABLE,
MakeRect(FirstDataRow, 5, DataRows, 1), ListGetDataPtr(CellStrings),
VAL_COLUMN_MAJOR);

// The following works but I shouldn't need to do this...
int items = ListNumItems(CellStrings), index, errorflag = FALSE;
char **Buf;

if ((Buf = malloc(items * sizeof(*Buf))) == NULL)
errorflag = TRUE; //memory allocation error
else
for (index = 0; index < items; index++)
if ((Buf[index] = malloc(MAX_STRING_LEN * sizeof(Buf))) == NULL)
errorflag = TRUE; //memory allocation error
else strcpy(Buf[index], ListGetPtrToItem (CellStrings, index));

if (!errorflag )
{
SetTableCellRangeVals (data_p, DATA_PANEL_TABLE,
MakeRect(FirstDataRow, MaxCols, DataRows, 1), Buf, VAL_COLUMN_MAJOR);
....
// other code to free memory and lists here...
}
-------

I've tried casting - (char **) ListGetDataPtr(CellStrings),in the
SetTableCellRangeVals function to avoid having to malloc temporary
storage, but I still get a general protection fault.

Any clarification on the use of ListGetDataPtr with
SetTableCellRangeVals for strings would be appreciated.

Thanks,
John.
0 Kudos
Message 1 of 2
(2,700 Views)
Hi John,

I believe that when you access ListGetDataPtr, you are returning a pointer to the first character of the string. When you call SetTableCellRangeVals, you are expected to send in a pointer to a pointer of a character. I believe this is why when you create a temp pointer variable and setting ListGetDataPtr to that, it works. By creating a temp pointer and sending in its address SetTableCellRangeVals, you are able to get the pointer to a pointer of a character. If you typecast ListGetDataPtr, you are just casting the data to a pointer of a character. If you do this, you still have to have a pointer point to that pointer of the char. The ideal way would be to pass the address of the pointer that is returned from ListGetDataPtr (i.e, SetTableCellRangeVals
(panelHandle, PANEL_TABLE, MakeRect(1,1,1,1), &(ListGetDataPtr(CellStrings)), VAL_COLUMN_MAJOR), but the compiler complains about that. So, the only way I found was to create a temporary pointer and pass its address. Hope this helps!

Jeremy L.
National Instruments
Jeremy L.
National Instruments
0 Kudos
Message 2 of 2
(2,700 Views)