NO

Author Topic: non-optimized compiling leads to access violation in Win32 console application  (Read 7453 times)

Franzki

  • Guest
For debugging purposes I set optimizations to 'none' while compiling my Win32 console program in Pelles C Version 5.00

The generated program then crashes with an "Access Violation" on the next line:

Code: [Select]
k_ptr->jaarkosten[jaar] = rentevoet * k_ptr-> bedrag + 0.5;

The generated exe file also crashes on other systems.

This is the structure I'm using:

Code: [Select]

typedef struct K_REGEL
{
struct K_REGEL *Next;
char type[16];
char omschrijving[100];
int jaarkosten[50];
int bedrag;
int investeringsjaar;
int looptijd;
int ncw;
} K_REGEL;

k_ptr is a pointer to a stucture K_REGEL

Code: [Select]
k_ptr=malloc(sizeof(K_REGEL));

In this case 'jaar' = 0

jaarkosten[jaar] has been initialised to 0 for all items in the array. Its also not the first time that I access it.

Does anyone have a clue what I'm doing wrong?

The generated program also crashes on other systems. No problems however when optimation is turned on.


JohnF

  • Guest
For debugging purposes I set optimizations to 'none' while compiling my Win32 console program in Pelles C Version 5.00

The generated program then crashes with an "Access Violation" on the next line:

Code: [Select]
k_ptr->jaarkosten[jaar] = rentevoet * k_ptr-> bedrag + 0.5;

The generated exe file also crashes on other systems.

This is the structure I'm using:

Code: [Select]

typedef struct K_REGEL
{
struct K_REGEL *Next;
char type[16];
char omschrijving[100];
int jaarkosten[50];
int bedrag;
int investeringsjaar;
int looptijd;
int ncw;
} K_REGEL;

k_ptr is a pointer to a stucture K_REGEL

Code: [Select]
k_ptr=malloc(sizeof(K_REGEL));

In this case 'jaar' = 0

jaarkosten[jaar] has been initialised to 0 for all items in the array. Its also not the first time that I access it.

Does anyone have a clue what I'm doing wrong?

The generated program also crashes on other systems. No problems however when optimation is turned on.

With optimization tuned on the generated code will often use a register instead of a memory location which can hide subtle bugs.

Did you check that malloc actually succeeds?

You should post a small app which manifests the problem, otherwise one cannot give good advice.

John

Franzki

  • Guest
I will try to isolate the problem... though it may be hard.

In the meantime:

If I replace:
Code: [Select]
k_ptr->jaarkosten[jaar] = rentevoet * k_ptr-> bedrag + 0.5;

with

Code: [Select]

int test;
test = rentevoet*k_ptr->bedrag+0.5;
k_ptr->jaarkosten[jaar]=test;

The problem is gone (in that part of the program).
But it returns somewhere else in the program at a similar line of code.

I also tried Pelles C 3.0 but can't reproduce the Access Violation error over there. everything seems to be fine there...

Franzki

  • Guest
Code: [Select]
#include<stdlib.h>

typedef struct K_REGEL
{
int jaarkosten[30];
int bedrag;
} K_REGEL;



int main()
{
K_REGEL *k_ptr;
float rentevoet;

if(    (k_ptr=malloc(sizeof(K_REGEL)))  !=NULL);

{
rentevoet=0.065;
k_ptr->bedrag=100;

k_ptr->jaarkosten[0]=rentevoet*k_ptr->bedrag+0.5;
}

return(0);

}


This should reproduce the Access Violation... easier than I thought...

Operation system used: WinXP home edition SP3 Dutch
« Last Edit: June 07, 2008, 02:30:03 PM by Franzki »

JohnF

  • Guest
I tried your listing - no access violation here.

Edit: with optimizations off.

John
« Last Edit: June 07, 2008, 03:38:52 PM by JohnF »

Franzki

  • Guest
I tried your listing - no access violation here.

Edit: with optimizations off.

John


I was about to ask that.. ;-)

Anyway... I ran the generated executable on several systems... Even on a 'clean' SP3 install on a Virtual Box and it gave the same error everytime.

Im not sure whether it's a WinXP SP3 issue, a Pelles C 5.0 issue, or just bad coding by me.

What version of Windows are you using John?

And should this discussion be moved to the BUG section?

JohnF

  • Guest
I tried your listing - no access violation here.

Edit: with optimizations off.

John


I was about to ask that.. ;-)

Anyway... I ran the generated executable on several systems... Even on a 'clean' SP3 install on a Virtual Box and it gave the same error everytime.

Im not sure whether it's a WinXP SP3 issue, a Pelles C 5.0 issue, or just bad coding by me.

What version of Windows are you using John?

And should this discussion be moved to the BUG section?

We don't know it's a bug yet, and personally I doubt it is.

I'm using XP SP3

I've found that problems like these are invariably caused by a variable being corrupted by a buffer overrun, or putting something somewhere it does not belong and so causing corruption.

John



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Here is that point in assembler:
Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
k_ptr->jaarkosten[0]=rentevoet*k_ptr->bedrag+0.5;
0040102B  |.  8B45 FC       MOV EAX,DWORD PTR SS:[k_ptr]
0040102E  |.  D945 F8       FLD DWORD PTR SS:[rentevoet]
00401031  |.  DA48 78       FIMUL DWORD PTR DS:[EAX+78]
00401034  |.  DC05 00304000 FADD QWORD PTR DS:[403000]               ; FLOAT 0.5000000000000000
0040103A  |.  E8 21060000   CALL 00401660
0040103F  |.  89C2          MOV EDX,EAX
00401041  |.  8910          MOV DWORD PTR DS:[EAX],EDX      <- here is that problem, same value in registers

This way it won't crash:
Code: [Select]
k_ptr->jaarkosten[0]=(int)rentevoet*k_ptr->bedrag+0.5;
Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
0040102B  |.  D945 F8       FLD DWORD PTR SS:[LOCAL.3]
0040102E  |.  E8 3D060000   CALL 00401670                            ; [Regel.00401670
00401033  |.  89C0          MOV EAX,EAX
00401035  |.  8B55 FC       MOV EDX,DWORD PTR SS:[LOCAL.2]
00401038  |.  0FAF42 78     IMUL EAX,DWORD PTR DS:[EDX+78]
0040103C  |.  50            PUSH EAX
0040103D  |.  DB0424        FILD DWORD PTR SS:[ESP]
00401040  |.  58            POP EAX
00401041  |.  DC05 00304000 FADD QWORD PTR DS:[403000]               ; FLOAT 0.5000000000000000
00401047  |.  E8 24060000   CALL 00401670                            ; [Regel.00401670
0040104C  |.  89C0          MOV EAX,EAX
0040104E  |.  8902          MOV DWORD PTR DS:[EDX],EAX
« Last Edit: June 07, 2008, 05:08:03 PM by timovjl »
May the source be with you

Franzki

  • Guest
Here's some simplified code...

I have also been able to reproduce it on a WinXP SP2 system with a clean Pelles 5.0 install so far.


Code: [Select]
#include<stdlib.h>
typedef struct TEST { int i[2]; } TEST;

int main()
{
TEST *ptr;
int t;
if(    (ptr=malloc(sizeof(TEST)))  !=NULL);
{
t=100;
ptr->i[0]=100;
ptr->i[1]=0.25* t;     //this works
ptr->i[1]=ptr->i[0];    //this works
ptr->i[1]=0.25*ptr->i[0];    // this causes an Access Violation
}
return(0);
}

<EDIT>


Adding an (int) to the calculation seems to avoid the problem as float 0.25 is converted to int 0.
However the aim of my calculation is to have a float calculation of which the result is converted to int.

<EDIT2>

Still no problems in Pelles C 3.0
« Last Edit: June 07, 2008, 06:12:14 PM by Franzki »

JohnF

  • Guest
Weird,

I guess Pelle should look at this. I still don't get an access violation.

John

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
I couldn't reproduce it either at first, but then I removed the /Ze option... Now I can...

OK, I will try and fix this one way or the other...
/Pelle

JohnF

  • Guest
Right - same here.

John

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
I have this fixed now, will upload a new version later.
/Pelle

JohnF

  • Guest
Thanks.

John

Franzki

  • Guest
The fix in version 5.00.1 seems to help, no problems anymore over here.

Thank you Pelle for fixing it so quick!


Frans