I don't understand this

Everything else that doesn't fall into one of the other PB categories.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

I don't understand this

Post 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?
BERESHEIT
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
@}--`--,-- A rose by any other name ..
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Fri Oct 28, 2005 4:04 pm, edited 1 time in total.
BERESHEIT
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

NetMaestro,

In the meantime, you mind this useful:

viewtopic.php?t=14503
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
BERESHEIT
Post Reply