Threading questions/problems

New to FreeBASIC? Post your questions here.
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions/problems

Postby fxm » Apr 23, 2019 15:46

The pointer passed to the thread has a local scope (the one of the function 'UpdateTaskStart()').
As soon as the function ends, the pointer is no longer valid.
So if the copy/conversion of this passed pointer in the thread is executed after the function end, this copy is corrupted.

A possible solution is to declare this pointer '[p]tData' in the main program, either as a shared variable or to pass it to the function as an additional parameter.
Iczer
Posts: 47
Joined: Jul 04, 2017 18:09

Re: Threading questions/problems

Postby Iczer » Apr 23, 2019 16:15

Thanks, I'm completely forgot about pointer going out of scope...
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions/problems

Postby fxm » Apr 24, 2019 8:18

Do you work under Windows ?
Because (with Windows 10) I can not highlight such behavior (when passing a local pointer to Threadcreate just before its going out of scope) on a short example.
Iczer
Posts: 47
Joined: Jul 04, 2017 18:09

Re: Threading questions/problems

Postby Iczer » Apr 24, 2019 12:38

i'm with XP64
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions/problems

Postby fxm » Apr 24, 2019 14:03

fxm wrote:A possible solution is to declare this pointer '[p]tData' in the main program, either as a shared variable or to pass it to the function as an additional parameter.

Has my proposed solution (or another) corrected your problem?
I doubt it by looking a second time, because the local pointer [p]tData ('Dim [p]tData As tagUpdateTask Ptr = New tagUpdateTask(...)') is in principle passed by value to 'Threadcreate()'.
fxm
Posts: 9076
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions/problems

Postby fxm » Apr 25, 2019 7:12

Maybe you start a multitude of simultaneous threads, and you arrive at a memory allocation limit (for Threadcreate but also for New).
Can you also try this principle with absolutely the fbc version 1.06.0 ('New' returns a null pointer if the memory allocation failed, only from the fbc version 1.06.0):

Code: Select all

Public Function UpdateTaskStart(iID As ULong, psMask As ZString Ptr, byref pwsParamA As WString Ptr, pwsParamB As WString Ptr, iParamC As ULong) As Long Export
   ' ----------------------------------------------------------------------------------------
   '
   ' ----------------------------------------------------------------------------------------
   Dim tData As tagUpdateTask Ptr = New tagUpdateTask(iID, psMask, pwsParamA, pwsParamB, iParamC)
   If tData = 0 Then                                                                               '' added
      Print "tagUpdateTask : allocation / failed : "                                               '' added
      Return xxxxxxxx                                                                              '' added
   Else                                                                                            '' added
      ' ..................................................................................
      Dim As Any Ptr ThreadHandle = ThreadCreate(@Thread_UpdateTask, tData, iThreadStackSize)
     
      If ThreadHandle = 0 Then
         Print "UpdateTaskStart Thread : start / failed : ";tagUpdateTask->Name
         Delete tData                                                                              '' added
         Return 40400003
      Else
         ThreadDetach(ThreadHandle)
      End If
      ' ----------------------------------------------------------------------------------------
   End If                                                                                          '' added
   Return 0
End Function

Otherwise, for fbc version 1.05.0 (or lower):

Code: Select all

Public Function UpdateTaskStart(iID As ULong, psMask As ZString Ptr, byref pwsParamA As WString Ptr, pwsParamB As WString Ptr, iParamC As ULong) As Long Export
   ' ----------------------------------------------------------------------------------------
   '
   ' ----------------------------------------------------------------------------------------
   'Dim tData As tagUpdateTask Ptr = New tagUpdateTask(iID, psMask, pwsParamA, pwsParamB, iParamC) '' suppressed
   Dim tData As tagUpdateTask Ptr = Callocate(Len(tagUpdateTask))                                  '' added
   If tData = 0 Then                                                                               '' added
      Print "tagUpdateTask : allocation / failed : "                                               '' added
      Return xxxxxxxx                                                                              '' added
   Else                                                                                            '' added
      tData->Constructor(iID, psMask, pwsParamA, pwsParamB, iParamC)                               '' added
      ' ..................................................................................
      Dim As Any Ptr ThreadHandle = ThreadCreate(@Thread_UpdateTask, tData, iThreadStackSize)
     
      If ThreadHandle = 0 Then
         Print "UpdateTaskStart Thread : start / failed : ";tagUpdateTask->Name
         Delete tData                                                                              '' added
         Return 40400003
      Else
         ThreadDetach(ThreadHandle)
      End If
      ' ----------------------------------------------------------------------------------------
   End If                                                                                          '' added
   Return 0
End Function

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 15 guests