www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Access violation in trying to use a class

reply gerleim <elf_qt _deletethisidyouarenotaspammer_yahoo.com> writes:
Wow, but the message is silly.
And hard to find - no file name and line number!
Just "Error: Access Violation".

It still works like this in version 7.51 Build 020.

Why the compiler does not give a decent error on this?

Sorry if it's something that is discussed, but I'm coming back to D after a long
pause.

gerleim
(ElfQT in the past)

== Repost the article of Derek Parnell (derek psych.ward)
== Posted at 2007/04/21 09:07 to D

On Sat, 21 Apr 2007 12:46:14 +0000 (UTC), Niovol wrote:

 I have written following code:
 ---------------------------
 import std.stdio;
 class Abc
 {
 public:
  int add(int a, int b) { return a + b; }
 }
 void main()
 {
  Abc abc;
  int a = abc.add(1, 2);
 }
 ----------------------------
 Upon compilling and building I executed a program. The message
 appeared: "Error: Access Violation". What should I do to use
 classes?

Firstly, this newsgroup is o longer active. Please use the group "digitalmars.D" next time. But back to your problem. Unlike C++, D requires that all classes be instantiated before use. That means, you must use a 'new' statement on your object before trying to access it. In D, a simple declaration such as your Abc abc; only allocates space for a null reference. This should work ... import std.stdio; class Abc { public: int add(int a, int b) { return a + b; } } void main() { Abc abc = new Abc; // Must be instantiated before use. int a = abc.add(1, 2); Abc def; int b; def = new Abc; // 'new' before use. b = abc.add(3, 4); } -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
May 28 2007
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 28 May 2007 21:23:17 +0000 (UTC), gerleim wrote:

 Wow, but the message is silly.
 And hard to find - no file name and line number!
 Just "Error: Access Violation".
 
 It still works like this in version 7.51 Build 020.
 
 Why the compiler does not give a decent error on this?

Because the compiler is not the agent that detects this problem. It is detected at run time by the hardware/operating system and by that time, things like source code line numbers are lost. The idea of having the compiler insert some run time checking, like it does for array bounds, has been asked for many times, but Walter seems to argue against that by saying we get the current run-time exception for free with the hardware so why do we also need software detection? The answer to that question is so we can get better error messages, but so far Walter isn't convinced that it is worth it. Walter wants us to use a debugger or use the source code divide-and-conquer method of bug detection instead, even though these are more time-consuming for us that using compiler generated checking. Such checking could be excluded with the -release switch like array bounds checking. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
May 28 2007
parent reply gerleim <elf_qt _deletethisifyouarenotaspammer_yahoo.com> writes:
Thanks for the quick reply.
Walter's point is wrong.
Newbie will come to D, newbie will forget to instantiate class, newbie will
wander
over "Error: Access Violation", newbie will wander off.

The first thing a usable IDE will do is to check this for itself.

The key thing is, if:
"It is detected at run time by the hardware/operating system and by that time,
things like source code line numbers are lost. "
then _because_ line number is lost to check it at another level. And the best
would be the compiler.

gerleim
(formerly ElfQT)
May 28 2007
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 28 May 2007 22:05:44 +0000 (UTC), gerleim wrote:


 Walter's point is wrong.

Yes, he has in this case (thankfully he doesn't get many things wrong).
 Newbie will come to D, newbie will forget to instantiate class, newbie will
wander
 over "Error: Access Violation", newbie will wander off.

Maybe ... but it does add an impediment to learning D and finding bugs. I figure though that Walter will see the use of compiler generated checking code eventually and slip it in as a present to his loyal subjects one day <G>. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
May 28 2007
parent reply Luis Marques <luismarques+spam gmail.com> writes:
Derek Parnell Wrote:

 I figure though that Walter will see the use of compiler generated checking
 code eventually and slip it in as a present to his loyal subjects one day
 <G>.

Instead of a per-access check, why doesn't the compiler add more intelligence to the exception handler to figure out the correct file:line, as ddbg does?
May 29 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Luis Marques wrote:
 Derek Parnell Wrote:
 
 I figure though that Walter will see the use of compiler generated checking
 code eventually and slip it in as a present to his loyal subjects one day
 <G>.

Instead of a per-access check, why doesn't the compiler add more intelligence to the exception handler to figure out the correct file:line, as ddbg does?

That would only work if you compile with debugging symbols. Plus, that's what (AFAIK) traced exceptions do. Now, if only we could get traced exceptions out of the box... -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 29 2007
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 28 May 2007 22:05:44 +0000 (UTC), gerleim wrote:

 The key thing is, if:
 "It is detected at run time by the hardware/operating system and by that time,
 things like source code line numbers are lost. "
 then _because_ line number is lost to check it at another level. And the best
 would be the compiler.

In the past, I'd tried some of the debuggers with D and had bad experience with them. I just gave ddbg another try and it is now quite useful. Test file: test.d class Foo { int xx; } void main() { Foo f; f.xx = 1; bool assign = false; int x; assert(assign); {x = 1;} } I compiled with "dmd test.d -g" the fired up ddbg with "ddbg test.exe". I used the "r" command and it ran the program and halted at the Access Exception... C:\temp>ddbg test.exe Ddbg 0.08.1 beta - D Debugger Copyright (c) 2007 Jascha Wetzel see http://ddbg.mainia.de/doc.html for documentation Loading symbols from test.exe ->r ntdll.dll loaded KERNEL32.dll loaded USER32.dll loaded GDI32.dll loaded IMM32.dll loaded ADVAPI32.dll loaded RPCRT4.dll loaded unknown DLL loaded OUTPUT DEBUG STRING: test.exe OUTPUT DEBUG STRING: Query the registry to get manually configured process list. Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at _Dmain test.d:8 ( 0x0040201e) ->q Which is exactly what I hoped it would show. So maybe now I can live without compiler code checking for access violations. THANKS JASCHA. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
May 28 2007
parent gerleim <elf_qt _deletethisifyouarenotaspammer_yahoo.com> writes:
 In the past, I'd tried some of the debuggers with D and had bad experience
 with them. I just gave ddbg another try and it is now quite useful.

 ...

 I compiled with "dmd test.d -g" the fired up ddbg with "ddbg test.exe".
 
 I used the "r" command and it ran the program and halted at the Access
 Exception...
 
 C:\temp>ddbg test.exe
 Ddbg 0.08.1 beta - D Debugger
 Copyright (c) 2007 Jascha Wetzel
 see http://ddbg.mainia.de/doc.html for documentation
 
 Loading symbols from test.exe
 ->r
 ntdll.dll loaded
 KERNEL32.dll loaded
 USER32.dll loaded
 GDI32.dll loaded
 IMM32.dll loaded
 ADVAPI32.dll loaded
 RPCRT4.dll loaded
 unknown DLL loaded
 OUTPUT DEBUG STRING:
 test.exe
 OUTPUT DEBUG STRING:
 Query the registry to get manually configured process list.
 Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at _Dmain
 test.d:8 (
 0x0040201e)
 ->q
 
 Which is exactly what I hoped it would show. So maybe now I can live
 without compiler code checking for access violations.
 
 
 THANKS JASCHA.
 
 -- 
 Derek Parnell
 Melbourne, Australia
 "Justice for David Hicks!"
 skype: derek.j.parnell

Oh, that's nice, thanks. gerleim
May 28 2007
prev sibling parent janderson <askme me.com> writes:
gerleim wrote:
 Wow, but the message is silly.
 And hard to find - no file name and line number!
 Just "Error: Access Violation".
 
 It still works like this in version 7.51 Build 020.
 
 Why the compiler does not give a decent error on this?
 
 Sorry if it's something that is discussed, but I'm coming back to D after a
long
 pause.
 
 gerleim
 (ElfQT in the past)
 
 == Repost the article of Derek Parnell (derek psych.ward)
 == Posted at 2007/04/21 09:07 to D
 
 On Sat, 21 Apr 2007 12:46:14 +0000 (UTC), Niovol wrote:
 
 I have written following code:
 ---------------------------
 import std.stdio;
 class Abc
 {
 public:
  int add(int a, int b) { return a + b; }
 }
 void main()
 {
  Abc abc;
  int a = abc.add(1, 2);
 }
 ----------------------------
 Upon compilling and building I executed a program. The message
 appeared: "Error: Access Violation". What should I do to use
 classes?

Firstly, this newsgroup is o longer active. Please use the group "digitalmars.D" next time. But back to your problem. Unlike C++, D requires that all classes be instantiated before use. That means, you must use a 'new' statement on your object before trying to access it. In D, a simple declaration such as your Abc abc; only allocates space for a null reference. This should work ... import std.stdio; class Abc { public: int add(int a, int b) { return a + b; } } void main() { Abc abc = new Abc; // Must be instantiated before use. int a = abc.add(1, 2); Abc def; int b; def = new Abc; // 'new' before use. b = abc.add(3, 4); } -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell

2 thoughts here: 1) If your running in debug it must be possible to query the debugging code to figure out where the error occured in code. 2) I think all error messages should come with a wiki page link. -Joel
May 28 2007