Pelles C forum

Pelles C => Bug reports => Topic started by: Franzki on June 04, 2009, 02:15:21 AM

Title: Exception: Access Violation
Post by: Franzki on June 04, 2009, 02:15:21 AM
I started with more complex code but even this works (edit: to reproduce the crash):


int main(void)
{
double atest[5][5];

atest[4][4]=10;

return(0);

}


PellesC6 RC2

I haven't been able to compile my project to a working exe so I have to stay in version 5.
I'm using some structures with arrays of doubles and ints and the program sometimes crashes accessing them... though I think they are within valid memory space.
Title: Re: Exception: Access Violation
Post by: AlexN on June 04, 2009, 07:36:54 AM
It seems that the optimzer optmizes away the intro of the function main, where the memory is allocated at the stack.  :(
Title: Re: Exception: Access Violation
Post by: Pelle on June 16, 2009, 09:07:12 PM
I will see if I can reproduce it...
Title: Re: Exception: Access Violation
Post by: japheth on June 17, 2009, 06:19:14 AM

I also got an access violation when I first tried PC v6rc2. Switching back to v5 helped.

There's no "stripped-down" test case, but the full project can be found at

http://www.japheth.de/download/jwasm196s.zip (http://www.japheth.de/download/jwasm196s.zip)

To make it, run

pomake -f Pellesc.mak

and then pocc.exe "should" crash when compiling file tokenize.c

japheth
Title: Re: Exception: Access Violation
Post by: Franzki on June 17, 2009, 10:13:32 AM
In my situation the exe file that is created causes the Acces Violation. It's not the IDE that crashes...
Title: Re: Exception: Access Violation
Post by: japheth on June 17, 2009, 10:48:51 AM
Quote from: Franzki on June 17, 2009, 10:13:32 AM
In my situation the exe file that is created causes the Acces Violation. It's not the IDE that crashes...

I'm sorry, I missed that. (Btw, it isn't the IDE, it's the compiler which crashes in my case.)
Title: Re: Exception: Access Violation
Post by: Franzki on June 23, 2009, 01:08:21 PM
Has anybody been able to reproduce this problem? (the compiler producing a crashing executable)

And is there a workaround available for Pelles 6 RC? I would really like to switch from my current Pelles 5 to 6 RC2 but I can't as long as my executables keep crashing.
Title: Re: Exception: Access Violation
Post by: nicolas.sitbon on June 23, 2009, 01:19:32 PM
I confirm the bug, I found 2 workaround for the moment:
- initialize the table at declaration (double atest[5][5] = {0};)
- in compiler option, disable all optimizations by setting to "none"
Title: Re: Exception: Access Violation
Post by: AlexN on June 23, 2009, 01:29:15 PM
Quote from: Franzki on June 23, 2009, 01:08:21 PM
Has anybody been able to reproduce this problem? (the compiler producing a crashing executable)

And is there a workaround available for Pelles 6 RC? I would really like to switch from my current Pelles 5 to 6 RC2 but I can't as long as my executables keep crashing.
You can switch off the optimizer. If you don't want to switch off the optmizer for the hole project you can try to switch on/off with #pragma optimize (none/time/size).

#pragma optimize(none)
int main(void)
{
double atest[5][5];
int i=0;
#pragma optimize(time)

atest[4][4]=10;

return(atest[4][4]);

}

or

int main(void)
{
static double atest[5][5];
int i=0;
atest[4][4]=10;

return(atest[4][4]);

}


And we have to wait for Pelles next version and test it then without #pragma or static.
Title: Re: Exception: Access Violation
Post by: Franzki on June 23, 2009, 01:45:58 PM
Hello Alex,

Thank you for your quick reaction...

Disabling optimation solves the problem for the given example... but it doesn't seem to work for my project, it still ends with an "Access Violation" And this doesn't happen in de Pelles 5 version.

Could there be some optimation going on, even when selecting "none"?
Title: Re: Exception: Access Violation
Post by: AlexN on June 23, 2009, 02:07:22 PM
Quote from: Franzki on June 23, 2009, 01:45:58 PM
Thank you for your quick reaction...
If I have time, no problem.

Does the solution of nicolas.sitbon (he posted nearly at the same time) and the version with static also don't help you?

Can you find with the debugger the point, where the program crashes?
Title: Better code example (Exception: Access Violation in compiled .exe)
Post by: Franzki on June 23, 2009, 02:48:00 PM

typedef struct K_REGEL
{
int jaarkosten[10];
} K_REGEL;



void main()
{
K_REGEL rekenblad;
K_REGEL * tptr;
tptr=&rekenblad;
int i;

for(i=0;i<10;++i)
rekenblad.jaarkosten[i]=0;

tptr->jaarkosten[1]=tptr->jaarkosten[0]; //  this does not crash
tptr->jaarkosten[1]=tptr->jaarkosten[0]*3.25+0.5; // <- here the program crashes

}


To make it more clear I tried to simplify the code as I used it in my project.

Regards,
Frans
Title: Re: Exception: Access Violation
Post by: Stefan Pendl on June 23, 2009, 02:54:40 PM
You can not fill an integer variable with a floating point value without type casting or converting the float into an int.
Title: Re: Exception: Access Violation
Post by: nicolas.sitbon on June 23, 2009, 02:56:47 PM
It seems that enabling "microsoft extensions" corrects the problem (with or without optimization). Can anyone confirm?
Title: Re: Exception: Access Violation
Post by: Franzki on June 23, 2009, 02:59:22 PM
tptr->jaarkosten[1]=(int)(tptr->jaarkosten[0]*3.25+0.5); // <- here the program crashes too

Hello Stephan,

I've tried that as well...  ;)
Title: Re: Exception: Access Violation
Post by: Franzki on June 23, 2009, 03:01:15 PM
Hello Nicolas...

You seem to be right about enabling the "Microsoft Extensions"
Title: Re: Exception: Access Violation
Post by: Franzki on June 23, 2009, 10:53:41 PM
Ok...

As far as I can see there seem to be 2 issues over here causing a runtime Access Violation error in PellesC 6RC2.

The first one:
void main()
{
  double atest[5][5];

  atest[4][4]=10;  // <- here the program crashes
}

This one can be avoided by disabling the compiler optimations.

The second one:
typedef struct K_REGEL
{
int jaarkosten[10];
} K_REGEL;

void main()
{
  K_REGEL rekenblad;
  K_REGEL * tptr;
  tptr=&rekenblad;
  int i;

  for(i=0;i<10;++i)
    rekenblad.jaarkosten[i]=0;

  tptr->jaarkosten[1]=(int)(tptr->jaarkosten[0]*3.25+0.5); // <- here the program crashes
}

This one can be avoided by enabling "microsoft extensions".

I hope Pelle is reading this and can shine his light on both issues.
Title: Re: Exception: Access Violation
Post by: AlexN on June 24, 2009, 07:52:36 AM
Quote from: Franzki on June 23, 2009, 10:53:41 PM

The second one:
typedef struct K_REGEL
{
int jaarkosten[10];
} K_REGEL;

void main()
{
  K_REGEL rekenblad;
  K_REGEL * tptr;
  tptr=&rekenblad;
  int i;

  for(i=0;i<10;++i)
    rekenblad.jaarkosten[i]=0;

  tptr->jaarkosten[1]=(int)(tptr->jaarkosten[0]*3.25+0.5); // <- here the program crashes
}

This one can be avoided by enabling "microsoft extensions".

For this one you can look into Pelles help file, for the extensions witch are enabled by this option.
One of this extensions is:
"Converting a floating-point number to signed int or long will chop rather than limit the value."
Title: Re: Exception: Access Violation
Post by: Franzki on July 13, 2009, 01:07:20 AM
Quote from: Franzki on June 23, 2009, 10:53:41 PM
Ok...

As far as I can see there seem to be 2 issues over here causing a runtime Access Violation error in PellesC 6RC2.

The first one:
void main()
{
 double atest[5][5];

 atest[4][4]=10;  // <- here the program crashes
}

This one can be avoided by disabling the compiler optimations.

The second one:
typedef struct K_REGEL
{
int jaarkosten[10];
} K_REGEL;

void main()
{
 K_REGEL rekenblad;
 K_REGEL * tptr;
 tptr=&rekenblad;
 int i;

 for(i=0;i<10;++i)
   rekenblad.jaarkosten[i]=0;

 tptr->jaarkosten[1]=(int)(tptr->jaarkosten[0]*3.25+0.5); // <- here the program crashes
}

This one can be avoided by enabling "microsoft extensions".

I hope Pelle is reading this and can shine his light on both issues.

Here's some good and some bad news...

Both samples (as far I could check) now both compile to a working executable in PellesC 6RC3. So it seems that Pelle managed to find and fix the problems regarding the code examples I posted.

The bad news is that my project still crashes with an Access Violation error. Workaround is enabling "Microsoft extensions".

There seems to be something going wrong with some combination of pointers, structures, arrays and float-to-int (or double-to-int) conversions in my project, causing Access Violation errors.

- No problems in PellesC 5
- No problems in other compilers (for example code::blocks)
- No problems in PellesC 6RC3 when enabling Microsoft Extensions

My full project is too big to post it here... I'm not sure if I can extract the essential code to reproduce the error.

Getting a bit desperate..  ???
Title: Re: Exception: Access Violation
Post by: AlexN on July 13, 2009, 08:40:41 AM
Quote from: Franzki on July 13, 2009, 01:07:20 AM
- No problems in other compilers (for example code::blocks)
Code::Blocks is not a compiler, it is just an IDE but usually bundeled with MinGW GCC as compiler. ;)

Quote from: Franzki on July 13, 2009, 01:07:20 AM
My full project is too big to post it here... I'm not sure if I can extract the essential code to reproduce the error.
Do you have the possibility to put the source-code on a server and send us the link?
Title: Re: Exception: Access Violation
Post by: Franzki on July 13, 2009, 09:27:29 AM
Quote from: AlexN on July 13, 2009, 08:40:41 AM
Quote from: Franzki on July 13, 2009, 01:07:20 AM
- No problems in other compilers (for example code::blocks)
Code::Blocks is not a compiler, it is just an IDE but usually bundeled with MinGW GCC as compiler. ;)

Quote from: Franzki on July 13, 2009, 01:07:20 AM
My full project is too big to post it here... I'm not sure if I can extract the essential code to reproduce the error.
Do you have the possibility to put the source-code on a server and send us the link?

Yes im using Code::Blocks with the MingW GCC compiler  ;)

I could put the code somewhere on a server but I prefer to strip it down to the essention... I will see what I can do  :P
Title: Re: Exception: Access Violation
Post by: Franzki on July 13, 2009, 10:31:37 AM
typedef struct K_REGEL
{
int jaarkosten[10];
} K_REGEL;

int main()
{
  K_REGEL rekenblad;
  K_REGEL * tptr;
  tptr=&rekenblad;
  int i;

  for(i=0;i<10;++i)
    rekenblad.jaarkosten[i]=0;

  tptr->jaarkosten[1]=(int)(tptr->jaarkosten[0]*3.25+0.5); // <- here the program crashes

  return(0);
}


I though all problems were gone with this piece of code...

It causes NO Access Violations when compiled with "default" console project settings in Pelles C 6RC3.

Though with optimation turned off it has an Access Violation again.

This can be avoided by enable Microsoft Extension when optimation is turned off.

By the way... this isn't my real problem with my project but I noticed it when I was trying to narrow down the other problems.
Title: Re: Exception: Access Violation
Post by: AlexN on July 13, 2009, 02:04:49 PM
Quote from: Franzki on July 13, 2009, 10:31:37 AM
typedef struct K_REGEL
  tptr->jaarkosten[1]=(int)(tptr->jaarkosten[0]*3.25+0.5); // <- here the program crashes
}


The compiler creates  without optimizing the following code for this line:

mov eax,dword [ebp+(-44)]
fild dword [eax]
fmul qword [(@10)]
fadd qword [(@11)]
fstp qword [ebp+(-56)]
fld qword [ebp+(-56)]
call ___ftol
mov edx,eax                                 // save the result of ___ftol from EAX of to EDX
mov dword [eax+(4)],edx                // store the result to ??? ;)


I think Pelle has to look at this.