www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - simple assignment statement compiles but becomes a run time error

reply "WhatMeWorry" <kheaser gmail.com> writes:
I've got a OpenGL function returning a pointer

// const GLubyte *version = glGetString(GL_VERSION);  // C++ and 
openLG code

// the following compiles with no errors or warnings

char openglVersion[100] = fromStringz(glGetString(GL_VERSION));  
// ABENDS HERE!


// documentation from std.string shows
// pure  system inout(char)[] fromStringz(inout(char)* cString);

// Seems pretty innocuous to me.


Debugger returns:
Unhandled exception at 0x76AC2F71 (KernelBase.dll) in 
WhatVersionOfOpenGL.exe
Jan 18 2015
parent reply "WhatMeWorry" <kheaser gmail.com> writes:
On Sunday, 18 January 2015 at 19:42:33 UTC, WhatMeWorry wrote:
 I've got a OpenGL function returning a pointer

 // const GLubyte *version = glGetString(GL_VERSION);  // C++ 
 and openLG code

 // the following compiles with no errors or warnings

 char openglVersion[100] = fromStringz(glGetString(GL_VERSION));
  // ABENDS HERE!


 // documentation from std.string shows
 // pure  system inout(char)[] fromStringz(inout(char)* cString);

 // Seems pretty innocuous to me.


 Debugger returns:
 Unhandled exception at 0x76AC2F71 (KernelBase.dll) in 
 WhatVersionOfOpenGL.exe
// Got it to work with writeln(fromStringz(glGetString(GL_VERSION))); But shouldn't I have been able to grabbed a copy of the string and put it in the openglVersion fixed size char array? Maybe string, or dynamic array?
Jan 18 2015
parent reply "weaselcat" <weaselcat gmail.com> writes:
On Sunday, 18 January 2015 at 19:51:02 UTC, WhatMeWorry wrote:
 On Sunday, 18 January 2015 at 19:42:33 UTC, WhatMeWorry wrote:
 I've got a OpenGL function returning a pointer

 // const GLubyte *version = glGetString(GL_VERSION);  // C++ 
 and openLG code

 // the following compiles with no errors or warnings

 char openglVersion[100] = fromStringz(glGetString(GL_VERSION));
 // ABENDS HERE!


 // documentation from std.string shows
 // pure  system inout(char)[] fromStringz(inout(char)* 
 cString);

 // Seems pretty innocuous to me.


 Debugger returns:
 Unhandled exception at 0x76AC2F71 (KernelBase.dll) in 
 WhatVersionOfOpenGL.exe
Hi, try replacing the fromStringz with to!string from std.conv
Jan 18 2015
parent reply "WhatMeWorry" <kheaser gmail.com> writes:
On Sunday, 18 January 2015 at 20:07:25 UTC, weaselcat wrote:
 On Sunday, 18 January 2015 at 19:51:02 UTC, WhatMeWorry wrote:
 On Sunday, 18 January 2015 at 19:42:33 UTC, WhatMeWorry wrote:
 I've got a OpenGL function returning a pointer

 // const GLubyte *version = glGetString(GL_VERSION);  // C++ 
 and openLG code

 // the following compiles with no errors or warnings

 char openglVersion[100] = 
 fromStringz(glGetString(GL_VERSION));
 // ABENDS HERE!


 // documentation from std.string shows
 // pure  system inout(char)[] fromStringz(inout(char)* 
 cString);

 // Seems pretty innocuous to me.


 Debugger returns:
 Unhandled exception at 0x76AC2F71 (KernelBase.dll) in 
 WhatVersionOfOpenGL.exe
Hi, try replacing the fromStringz with to!string from std.conv
No cigar. char openglVersion[100] = fromStringz(glGetString(GL_VERSION)); causes the same exception. Thanks for suggestion. Could (Can?) OpenGL be marking the string as read only? So we can print out value put not copy it?
Jan 18 2015
parent reply Mike Parker <aldacron gmail.com> writes:
On 1/19/2015 10:44 AM, WhatMeWorry wrote:
 On Sunday, 18 January 2015 at 20:07:25 UTC, weaselcat wrote:
 On Sunday, 18 January 2015 at 19:51:02 UTC, WhatMeWorry wrote:
 On Sunday, 18 January 2015 at 19:42:33 UTC, WhatMeWorry wrote:
 I've got a OpenGL function returning a pointer

 // const GLubyte *version = glGetString(GL_VERSION);  // C++ and
 openLG code

 // the following compiles with no errors or warnings

 char openglVersion[100] = fromStringz(glGetString(GL_VERSION));
 // ABENDS HERE!


 // documentation from std.string shows
 // pure  system inout(char)[] fromStringz(inout(char)* cString);

 // Seems pretty innocuous to me.


 Debugger returns:
 Unhandled exception at 0x76AC2F71 (KernelBase.dll) in
 WhatVersionOfOpenGL.exe
Hi, try replacing the fromStringz with to!string from std.conv
No cigar. char openglVersion[100] = fromStringz(glGetString(GL_VERSION)); causes the same exception. Thanks for suggestion. Could (Can?) OpenGL be marking the string as read only? So we can print out value put not copy it?
You're trying to shove a dynamic array into a static array. That isn't going to fly. Your openglVersion variable needs to be dynamic. Declare it as "string" or "auto": auto openglVersion = fromStringz( glGetString( GL_Version )); Note that fromStringz returns a slice of the C string, which means that if the C string doesn't survive after the return call, or if you plan to keep your D string around outside of the call site, you can get a crash. The OpenGL specification requires strings returned by glGetString to be static, i.e. the pointer will always be valid, so you can do whatever you want. Generally, though, to!string is what you want. It allocates memory for the string and copies it.
Jan 18 2015
parent "jpkl" <jpkl nowhere.fr> writes:
On Monday, 19 January 2015 at 02:04:23 UTC, Mike Parker wrote:
 On 1/19/2015 10:44 AM, WhatMeWorry wrote:
 On Sunday, 18 January 2015 at 20:07:25 UTC, weaselcat wrote:
 On Sunday, 18 January 2015 at 19:51:02 UTC, WhatMeWorry wrote:
 On Sunday, 18 January 2015 at 19:42:33 UTC, WhatMeWorry 
 wrote:
 I've got a OpenGL function returning a pointer

 // const GLubyte *version = glGetString(GL_VERSION);  // 
 C++ and
 openLG code

 // the following compiles with no errors or warnings

 char openglVersion[100] = 
 fromStringz(glGetString(GL_VERSION));
 // ABENDS HERE!


 // documentation from std.string shows
 // pure  system inout(char)[] fromStringz(inout(char)* 
 cString);

 // Seems pretty innocuous to me.


 Debugger returns:
 Unhandled exception at 0x76AC2F71 (KernelBase.dll) in
 WhatVersionOfOpenGL.exe
Hi, try replacing the fromStringz with to!string from std.conv
No cigar. char openglVersion[100] = fromStringz(glGetString(GL_VERSION)); causes the same exception. Thanks for suggestion. Could (Can?) OpenGL be marking the string as read only? So we can print out value put not copy it?
You're trying to shove a dynamic array into a static array. That isn't going to fly. Your openglVersion variable needs to be dynamic. Declare it as "string" or "auto": auto openglVersion = fromStringz( glGetString( GL_Version )); Note that fromStringz returns a slice of the C string, which means that if the C string doesn't survive after the return call, or if you plan to keep your D string around outside of the call site, you can get a crash. The OpenGL specification requires strings returned by glGetString to be static, i.e. the pointer will always be valid, so you can do whatever you want. Generally, though, to!string is what you want. It allocates memory for the string and copies it.
the array type helpers _.idup_ and _.dup_ can also be used to have a safe copy: `` auto openglVersion = glGetString(GL_Version).fromStringz.idup; ``
Jan 18 2015