Page 1 of 1

I don't understand this

Posted: Fri Oct 28, 2005 12:24 am
by netmaestro
Take this code and compile it to a shared dll:

Code: Select all

ProcedureDLL.s WordExists()
a = Random(1)
If a = 1
  ProcedureReturn "FOUND"
Else
  ProcedureReturn "NOT FOUND"
EndIf
EndProcedure
Then run this test code which calls the dll:

Code: Select all

OpenLibrary(0,"d:\books\ttest.dll")
For i = 1 To 9909
  a$=PeekS(CallFunction(0,"WordExists"))
Next i
Works fine. Now put in any number above 9909 and it dies on "invalid memory access". Can someone tell me why?

Posted: Fri Oct 28, 2005 1:35 am
by Dare2
Hi netmaestro,

First thought is that the dll should return a string in a global var. So:

Code: Select all

Global rv$
ProcedureDLL.s WordExists()
;rv$=""
  a = Random(1)
  If a = 1
    rv$="FOUND"
  Else
    rv$="NOT FOUND"
  EndIf
  ProcedureReturn rv$
EndProcedure
However I tried that and still got an error. Error can occur with numbers under 9909, after repeated runs. The value returned (peeked) increments with each call, by the length of the returned string, so perhaps this is a memory leak problem?

I also tried clearing the return string first (commented line above) it made no difference.

So this post doesn't help you much, does it? Sorry. But maybe it helps track a bug, or maybe it helps next poster to see where we both have it wrong.

Posted: Fri Oct 28, 2005 10:39 am
by GedB
It looks to me that a stack is overflowing. Every call is pushing onto a stack somewhere but never popping anything off.

The stack is then running out of space.

You should post this as a bug report.

Posted: Fri Oct 28, 2005 3:16 pm
by netmaestro
I've emailed Fred with the link to this topic. Actually 9909 is not cast in stone as I found that one after several failed runs and the number seems to change after more runs. If it turns out to be a bug, knowing the folks at Fantaisie Software, it'll probably be fixed before sundown. MS would schedule it for the next service pack in six months and charge me for the support.

Posted: Fri Oct 28, 2005 3:42 pm
by GedB
NetMaestro,

In the meantime, you mind this useful:

viewtopic.php?t=14503

Posted: Fri Oct 28, 2005 4:03 pm
by netmaestro
Ah. Thank you very much, GedB. I thought it might be that I just wasn't using the language correctly, which is why I didn't post this in the bug section. The example rewritten this way works perfectly:

dll:

Code: Select all

ProcedureDLL.s WordExists(*stringloc)
a = Random(1)
If a = 1
  PokeS(*stringloc, "FOUND")
Else
  PokeS(*stringloc, "NOT FOUND")
EndIf
ProcedureReturn
EndProcedure
And the calling program:

Code: Select all

*stringloc = AllocateMemory(100)
OpenLibrary(0,"d:\books\ttest.dll") 
For i = 1 To 200000
  CallFunction(0,"WordExists",*stringloc)
  a$=PeekS(*stringloc)
Next i