www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Access violations, reloaded

reply mike <vertex gmx.at> writes:
Hi!

First off I wanted to say how great the new features are - D is getting =
 =

more and more impressive. On a side note it's funny that by reading your=
  =

discussions about Ruby iterators I got interested in Ruby, had a look in=
to  =

it and I was very impressed too. It seems that Ruby and D are both quite=
  =

on the cutting edge of programming languages these days.

Anyway, here's the problem:

(I posted about that some time ago in the D.learn ng, I hope it's ok to =
 =

follow up on it here. Code is pseudo-code typed from memory.)

I have a function that gets a C string from a DLL (it's a byte *, but th=
e  =

plugin works with char * internally). Host provides the buffer, plugin  =

fills it:

' char[] getDisplayValue()
' {
'      byte[2000] temp;
'      pluginInstance.dispatch(GET_VALUE, &temp[0]);
'      return myConvertFunc(temp);
' }

myConvertFunc looks like that:

' char[] myConvertFunc(byte *buffer)
' {
'      return strip(toString(cast(char *)buffer)); // When I .dup here i=
t  =

crashes even faster!
' }

Then I have the GUI render code:

' [...]
' surface =3D SDL_RenderText(toStringz(getDisplayValue));
' SDL_BlitSurface(surface, position);
' SDL_FreeSurface(surface);
' [...]

Somewhere in this lurks an Access Violation which occurs randomly EITHER=
  =

in the convert function, when getDisplayValue wants to return the value,=
  =

in the SDL_RenderText OR when blitting the surface onto the screen. It i=
s  =

NOT reproducable exactly where it happens, but it happens roughly after =
 =

the same time (about a minute or two, never actually timed it) each run.=
  =

Memory goes up constantly while it's running. My best guess is that  =

there's a problem with local variables, .dup-ing them and the GC which  =

collects at the wrong time.

I now rewrote it like that:

' void getDisplayValueZ(byte *buffer)
' {
'     pluginInstance.dispatch(GET_VALUE, buffer);
' }

And in the render code:

' [...]
' byte[2000] buffer;
' getDisplayValueZ(&buffer[0]);
' surface =3D SDL_RenderText(cast(char *)&buffer[0]);
' SDL_BlitSurface(surface, position);
' SDL_FreeSurface(surface);
' [...]

That works. No Access Violation, no increase in memory usage.

Anyway, I'm still curious about it. I understand why there's no increase=
  =

in memory usage in the second version, but I don't understand why there'=
s  =

an Access Violation in the first version to begin with. There must be so=
me  =

fundamental issue that I didn't quite get yet, those Access Violations  =

bite me every now and then. Any hint would be great.

Regards,
Mike

-- =

Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/=
mail/
Nov 15 2006
parent Walter Bright <newshound digitalmars.com> writes:
mike wrote:
 ' char[] getDisplayValue()
 ' {
 '      byte[2000] temp;
 '      pluginInstance.dispatch(GET_VALUE, &temp[0]);
 '      return myConvertFunc(temp);
 ' }

Looks like you're returning a reference to a local variable (temp). This will cause erratic behavior, probably crashing.
Nov 16 2006