www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Stange Exceptions with Delegates

reply Trevor Parscal <trevorparscal hotmail.com> writes:
Im experiencing a very strange behavior with delegates. When using some OpenGL
functions in a delegate my program will give me

object.Exception: Access Violation
- when there's a Cout call at the begining and end of the delegate

object.Exception: Win32 Exception
- when it's only OpenGL calls in the delegate

The strangest part is that the Cout calls will succeed in printing to the
console, meaning the access violation isn't in the OpenGL calls. I also placed
some Cout calls before and after the actual calling of the delegate, and the
first prints, while the second (post delegate calling) never does.

So what could be happening between the last call inside of a delegate and the
delegate being done?

Are there any other methods of tracking down the problem?
Jan 11 2008
parent reply torhu <no spam.invalid> writes:
Trevor Parscal wrote:
 Im experiencing a very strange behavior with delegates. When using some OpenGL
functions in a delegate my program will give me
 
 object.Exception: Access Violation
 - when there's a Cout call at the begining and end of the delegate
 
 object.Exception: Win32 Exception
 - when it's only OpenGL calls in the delegate
 
 The strangest part is that the Cout calls will succeed in printing to the
console, meaning the access violation isn't in the OpenGL calls. I also placed
some Cout calls before and after the actual calling of the delegate, and the
first prints, while the second (post delegate calling) never does.
 
 So what could be happening between the last call inside of a delegate and the
delegate being done?

Are you on windows? A typical mistake is calling openGL functions using the cdecl calling convention, you need to use stdcall. stdcall is 'extern (Windows)' or 'extern (System)' in D. Mixing up calling conventions leads to stack corruption, and then usually a crash sooner or later after the function returns.
Jan 11 2008
parent reply Trevor Parscal <trevorparscal hotmail.com> writes:
torhu Wrote:

 Trevor Parscal wrote:
 Im experiencing a very strange behavior with delegates. When using some OpenGL
functions in a delegate my program will give me
 
 object.Exception: Access Violation
 - when there's a Cout call at the begining and end of the delegate
 
 object.Exception: Win32 Exception
 - when it's only OpenGL calls in the delegate
 
 The strangest part is that the Cout calls will succeed in printing to the
console, meaning the access violation isn't in the OpenGL calls. I also placed
some Cout calls before and after the actual calling of the delegate, and the
first prints, while the second (post delegate calling) never does.
 
 So what could be happening between the last call inside of a delegate and the
delegate being done?

Are you on windows? A typical mistake is calling openGL functions using the cdecl calling convention, you need to use stdcall. stdcall is 'extern (Windows)' or 'extern (System)' in D. Mixing up calling conventions leads to stack corruption, and then usually a crash sooner or later after the function returns.

Excelent advice, and after looking at other opengl bindings you are absolutely right... However. I am using implib to generate a .lib from my opengl32.dll on my system - and the only way I can get a compile without linking errors is to use extern(C) I have tried using the /s flag and not - no difference in behavior. Any ideas of what I could do to fix this?
Jan 12 2008
parent Trevor Parscal <trevorparscal hotmail.com> writes:
Trevor Parscal Wrote:

 torhu Wrote:
 
 Trevor Parscal wrote:
 Im experiencing a very strange behavior with delegates. When using some OpenGL
functions in a delegate my program will give me
 
 object.Exception: Access Violation
 - when there's a Cout call at the begining and end of the delegate
 
 object.Exception: Win32 Exception
 - when it's only OpenGL calls in the delegate
 
 The strangest part is that the Cout calls will succeed in printing to the
console, meaning the access violation isn't in the OpenGL calls. I also placed
some Cout calls before and after the actual calling of the delegate, and the
first prints, while the second (post delegate calling) never does.
 
 So what could be happening between the last call inside of a delegate and the
delegate being done?

Are you on windows? A typical mistake is calling openGL functions using the cdecl calling convention, you need to use stdcall. stdcall is 'extern (Windows)' or 'extern (System)' in D. Mixing up calling conventions leads to stack corruption, and then usually a crash sooner or later after the function returns.

Excelent advice, and after looking at other opengl bindings you are absolutely right... However. I am using implib to generate a .lib from my opengl32.dll on my system - and the only way I can get a compile without linking errors is to use extern(C) I have tried using the /s flag and not - no difference in behavior. Any ideas of what I could do to fix this?

I found these def files as referred to in this post http://www.digitalmars.com/d/archives/digitalmars/D/12896.html which solved my problem. Thanks for the help!
Jan 12 2008