Access violations, reloaded
mike
vertex at gmx.at
Wed Nov 15 07:40:54 PST 2006
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 into
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 the
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 it
crashes even faster!
' }
Then I have the GUI render code:
' [...]
' surface = 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 is
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 = 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 some
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ärem E-Mail-Modul: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list