LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

General Protection Fault Error

Hi
 
I am getting General Protection Fault (GPF) Error when i am trying to read from dynamically allocated string array variable. There are two variable of same type and with one is giving GPF error. I tried and replace with fixed size char array instead of dynamically allocated string array variable but error remains
any idea...
 
Thanks and Regards
Rakesh
0 Kudos
Message 1 of 14
(4,730 Views)

If you run the program in the debug mode you should get some useful clues as to what is going on.

JR

0 Kudos
Message 2 of 14
(4,730 Views)

hello JR

Thanks...My application is in debug mode. and i have put the break point where i am getting this error. When i take the cursor over the variable before executining i find expected value in array but as soon as i execute that perticular statement i get an error.

for (channelNumber =0;channelNumber<PSPU1Channels;channelNumber++)
 {

        
         strcpy(PSChNum, PSPU1ChannelNum[channelNumber]);

 

 

0 Kudos
Message 3 of 14
(4,726 Views)

I am sorry by mistake "Submit Post" clicked..

hello JR

Thanks...My application is in debug mode. and i have put the break point where i am getting this error. When i take the cursor over the variable before executining i find expected value in array but as soon as i execute that perticular statement i get an error.

 

for (channelNumber =0;channelNumber<PSPU1Channels;channelNumber++)
 {

        // strcpy(PSChNum, channels.PSPU1PSNum[channelNumber]); //Original statement here PSPU1ChannelNum[channelNumber] show NULL this is the                         // case where memory allocated dynamically and channels.PSPU1PSNum[channelNumber] show NULL before executing this statment
         strcpy(PSChNum, PSPU1ChannelNum[channelNumber]);  //Modified  PSPU1ChannelNum[channelNumber] shows the expected value  executing this  statement  PSPU1ChannelNum[channelNumber] is a two dimensional array of char   (char PSPU1ChannelNum[64][5]; )

.................

.............

}

I have very similar one more block where i don't find any error.

Thanks and Regards

Rakesh

 

0 Kudos
Message 4 of 14
(4,725 Views)

At the risk of losing more brownie points Smiley Sad What is the typical content of the strings you are copying? If they are not terminated with a \0 character then strcpy() will not stop copying bytes until it finds one somewhere - this is often a source of unexpected program crashes, as the function just ploughs on, creating havoc by overwriting data or even running off the end of the memory space allocated to the program. You could use strncpy() to limit the number of characters copied.

JR

0 Kudos
Message 5 of 14
(4,719 Views)
Hello JR
 
Thanks for prompt reply and help. As suggested i made the necessary modification and used "strncpy"  but my problem still exists. As i also mentioned that i  used fixed defined array of string (char PSPU1ChannelNum[64][5];) to avoid any problem which may be causing due dynamic allocation.
 
Regards
Rakesh
0 Kudos
Message 6 of 14
(4,706 Views)

Perhaps if you posted some more of your source code it might be a bit clearer as to what is supposed to be happening?

JR

0 Kudos
Message 7 of 14
(4,703 Views)

Hello JR

As suggested i am attaching the source code of my application. Please refer function LogPSStatus() in  Datalogging.c. By using fixed defined array of string (PSPU1ChannelNum) i don't find GPF Error. I am sorry for my previous comments in which i had mentioned of persistence of error in 2nd case.  That time i have not used "strncpy()" function all the places so i was getting GPF Error.  Now please help me to investigate GPF error as i intend to allocated memory dynamically.

Thanks and Regards

Rakesh

0 Kudos
Message 8 of 14
(4,699 Views)


RKGupta wrote:

<snip>     

channels.PSPU1PSNum[channelNumber] show NULL before executing this statment

<snip>


This is clearly the cause of the GPF; the problem is I cannot see why this pointer should be NULL - after all you appear to calloc adequately on line 1400 of ParseConfigFiles.c. Two things spring to mind:

  1. The calloc operation actually failed for some reason (unlikely) - you could check for a NULL assignment at the point of allocation ie in line 1400 and break or print an error message if it does fail.
  2. The strncpy routine is using a larger value of PSPU1Channels (via the loop variable channelNumber) to the one used in the allocation loop - but I would guess that you effectively treat this variable as a constant once its inital value has been established somewhere?

Unfortunately I could not build the program on my PC (missing files) so I was not able to see the GPF in action. Maybe if you set a breakpoint at line 1406 and check that all the allocations have succeeded? At what value of the loop counter channelNumber does the GPF occur? Does this give you any clues?

Good luck!

JR

Message 9 of 14
(4,683 Views)
Hello JR
 
Thanks for your valuable suggestion. As you mentioned it is obvious to get GPF once accessing pointer which is already assign to NULL. I am trying to debug where channels.PSPU1PSNum is getting assign NULL but so far i am not able to trace.
As you suggested i have checked for NULL assignment but there is no NULL assignment. I determine the value only once for PSPU1Channels in function ParsePsChannelLine () and this value remains until updateStatus[PS_CONFIG_FILE] (refer line 882 of Parseconfig.c) value changed, which i am keeping constant after the application initialized. Any clue to find out NULL assignment for channels.PSPU1PSNum variable. I tried with putting this variable into Add Watch Expression. I set check options 1 to break when value changes  2 Update display Continuously and tried different scope like Global to File, Global to project. But i am not getting any break for NULL assignment.
Many thanks for your continuous help.
 
Regards
Rakesh
 
 
0 Kudos
Message 10 of 14
(4,671 Views)