[Gambas-user] Crash with Free()

Demosthenes Koptsis demosthenesk at ...626...
Fri Jan 14 19:29:35 CET 2011


Jussi i can post here the cases of pointer's mis-usage from my book and
you can add any of them in your project freely.

1) Assign new address to an existing pointer drives to memory leak.
The first address cannot be freed directly.

' Gambas module file

Public Sub Main()

Dim pPointer1 As Pointer
Dim pPointer2 As Pointer

pPointer1 = Alloc(4)
pPointer2 = Alloc(4)

Print "Address of pPointer1"; pPointer1
Print "Address of pPointer2"; pPointer2

pPointer2 = pPointer1

Print "Address of pPointer1"; pPointer1
Print "Address of pPointer2"; pPointer2

Free(pPointer1)
Free(pPointer2)

End


2) Not freeing a pointer.

' Gambas module file

Public Sub Main()
  
  UsePointer()
  
End

Public Sub UsePointer()

Dim pPointer1 As Pointer

pPointer1 = Alloc(4)

Print "Address of pPointer1"; pPointer1

End

3) Write to a pointer greater length of data, buffer overflow

' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream

pPointer = Alloc(4)
hMemory = Memory pPointer For Read Write
  
Write #hMemory, CByte(0) As Byte
Write #hMemory, CByte(1) As Byte
Write #hMemory, CByte(0) As Byte
Write #hMemory, CByte(1) As Byte
Write #hMemory, CByte(0) As Byte
Close #hMemory

Free(pPointer)

End

4) usage o pointer which has been freed 
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream

pPointer = Alloc(4)
hMemory = Memory pPointer For Read Write
  
Write #hMemory, CByte(0) As Byte
Write #hMemory, CByte(1) As Byte
Write #hMemory, CByte(0) As Byte
Write #hMemory, CByte(1) As Byte
Close #hMemory

Print Byte@(pPointer)

Free(pPointer)

Print Byte@(pPointer)

End

5) Free an already free pointer

' Gambas module file

Public Sub Main()

Dim pPointer As Pointer

pPointer = Alloc(4)

Free(pPointer)

Free(pPointer)

End

6) Free unallocated pointer.
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer

pPointer = Alloc(4)

Print pPointer

pPointer += 10

Print pPointer

'Free(pPointer)

End

7) Write to a pointer which has been freed.
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream

pPointer = Alloc(SizeOf(gb.Integer))
hMemory = Memory pPointer For Read Write

Print pPointer

Free(pPointer)

Write #hMemory, 10 As Integer

Print pPointer
Print Int@(pPointer)
Print pPointer

End

8) Write to unallocated memory
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream

pPointer = Alloc(SizeOf(gb.Integer))
hMemory = Memory pPointer For Read Write

Print pPointer

pPointer += 10

Print pPointer

Write #hMemory, 10 As Integer

Free(pPointer)

End

---------
Suggested usage of pointers
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream

pPointer = Alloc(4)

Print pPointer

hMemory = Memory pPointer For Read Write
  
Write #hMemory, CByte(0) As Byte
Write #hMemory, CByte(1) As Byte
Write #hMemory, CByte(0) As Byte
Write #hMemory, CByte(1) As Byte

Close #hMemory
Free(pPointer)
pPointer = 0

Print pPointer
Print Byte@(pPointer) 'expected error

End


On Fri, 2011-01-14 at 19:23 +0200, Jussi Lahtinen wrote:
> True, and that is my only usage for them,
> except GambasTester which is supposed to test every command
> for possible errors.
> 
> Jussi
> 
> 
> On Fri, Jan 14, 2011 at 05:29, Ian Haywood <ihaywood at ...1979...> wrote:
> 
> > On Fri, Jan 14, 2011 at 8:41 AM, Demosthenes Koptsis
> > <demosthenesk at ...626...> wrote:
> > > :) Pointers are tricky!
> > yes, the ability to avoid pointers is one of the main advantages of
> > higher-level languages like Gambas.
> > I'm curious to know why you guys are so fascinated by them, other than
> > interfacing with C libraries I would have thought they should be left
> > well alone.
> >
> > Ian
> >
> >
> > ------------------------------------------------------------------------------
> > Protect Your Site and Customers from Malware Attacks
> > Learn about various malware tactics and how to avoid them. Understand
> > malware threats, the impact they can have on your business, and how you
> > can protect your company and customers by using code signing.
> > http://p.sf.net/sfu/oracle-sfdevnl
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> ------------------------------------------------------------------------------
> Protect Your Site and Customers from Malware Attacks
> Learn about various malware tactics and how to avoid them. Understand 
> malware threats, the impact they can have on your business, and how you 
> can protect your company and customers by using code signing.
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

-- 
Regards,
Demosthenes Koptsis.





More information about the User mailing list