Author Topic: function pointers and memory leacks  (Read 5085 times)

0 Members and 2 Guests are viewing this topic.

pber

  • Guest
function pointers and memory leacks
« on: July 01, 2014, 05:33:39 AM »
I can not entirely free instances of this class.

Code: [Select]
class IFunction
    string id
    sys address
[b]    declare function ptr f()[/b]
   
    method toString() as string
        return "f:" id "<" address ">"
    end method
   
    method constructor(id0 as string, sys addr0)
        initialize id0,addr0
    end method

    method destructor()
        frees this.id
        frees this.address
    end method

    method initialize(id0 as string, sys addr0)
        id      =id0
        address =addr0
    end method

    method call()
        @f=address
        f()
    end method
end class

I suspect the function-pointer declaration allocates
some global memory at each instance creation.
Memory I can not free in the destructor.


Charles Pegge

  • Guest
Re: function pointers and memory leacks
« Reply #1 on: July 01, 2014, 09:51:29 AM »
Hi Paolo,

frees is required only for deallocating bstrings.

Oxygen strings are always automatically garbage-collected.

Therefore in your example, the destructor has nothing to do. But after calling the destructor, del will deallocate memory allocated to the object itself and set the object pointer to 0.

Test:
Code: [Select]
class IFunction
    string id
    sys address

    declare function ptr f()
   
    method toString() as string
        return "f:" id "<" address ">"
    end method
   
    method constructor(id0 as string, sys addr0)
        initialize id0,addr0
    end method

    method destructor()
    end method

    method initialize(id0 as string, sys addr0)
        id      =id0
        address =addr0
    end method

    method call()
        @f=address
        f()
    end method
end class

function f1() as sys, label
print "here"
end function

new Ifunction fn("ABC",@f1)
print fn.toString
fn.call
del fn
print @fn '0

PS: the new and del macros:

Code: [Select]
def new 1 %1 * %2 : @%2=getmemory sizeof %1 : %2.constructor
def del 1 %1.destructor : freememory @%1 : @%1=0


PS: Leeks

national vegetable for Wales :)


« Last Edit: July 01, 2014, 11:58:49 AM by Charles Pegge »

pber

  • Guest
Re: function pointers and memory leacks
« Reply #2 on: July 01, 2014, 01:13:25 PM »
A soup of leeks, potatoes and celery
and I feel like a king.

...well: I know it was a non-sense trying to free local strings.
But in effect it recuces the memory leack.

Anyway: no, still loosing memory.
Maybe I use the wrong test method (even if for other classes
it registers no leacks at all). It's not a great tool: I use sysinternals processExplorer.

Which tool do you use to test memory usage?

JRS

  • Guest
Re: function pointers and memory leacks
« Reply #3 on: July 01, 2014, 01:43:54 PM »
Quote
Which tool do you use to test memory usage?

Under Windows, if I start getting messages saying I've exhausted resources and my mouse hops around the screen, it's a clue for me that something is wrong. Then again it's Windows so maybe not. REBOOT tends to give a second chance to watch it happen all over again.



pber

  • Guest
Re: function pointers and memory leacks
« Reply #4 on: July 01, 2014, 01:54:37 PM »
Under Windows

I know: the win logo at startup is there to alert something is wrong (or it will be shortly).

JRS

  • Guest
Re: function pointers and memory leacks
« Reply #5 on: July 01, 2014, 02:36:36 PM »
Can you imagine the money Microsoft could have made on virus protection? Those pop-ups that won't let you do any else until you buy a fix malware marketing campaigns would have netted a fortune. Missed opportunities.  :-X

Charles Pegge

  • Guest
Re: function pointers and memory leacks
« Reply #6 on: July 01, 2014, 03:21:54 PM »
Yes, I see the memory leak (in Windows Task Manager) when creating and deleting instances  of Ifunction many times.

For every instance, The id string is logged onto the global garbage list which is not emptied till the end of the program. Not much help for this mode of OOP.

The cleanest solution is to make id a bstring, then free it explicitly in the destructor, like you did before.

Code: [Select]
class IFunction
    bstring id
    sys address

    declare function ptr f()
   
    method toString() as string
        return "f:" id "<" address ">"
    end method
   
    method constructor(id0 as string, sys addr0)
        initialize id0,addr0
    end method

    method destructor()
    frees id
    end method

    method initialize(id0 as string, sys addr0)
    'method initialize(string id0, sys addr0)
        #show id      =id0
        address =addr0
    end method

    method call()
        @f=address
        f()
    end method
end class

function f1() as sys, label
'print "here"
end function

print "start"
string s
for i=1 to 1000000
  new Ifunction fn("ABC",@f1)
  s=fn.toString
  fn.call
  del fn
 
next
print @fn '0


PS: Wikileaks :)



If Julian Assange steps out of the Equadorian embassy, these chaps will arrest him immediately. The siege has cost 10 million pounds so far! Someone must be desperate.
« Last Edit: July 01, 2014, 04:02:56 PM by Charles Pegge »

pber

  • Guest
Re: function pointers and memory leacks
« Reply #7 on: July 01, 2014, 04:12:36 PM »
For every instance, The id string is logged onto the global garbage list which is not emptied till the end of the program. Not much help for this mode of OOP.

The cleanest solution is to make id a bstring, then free it explicitly in the destructor, like you did before.

Thanks again Charles,
this way it's just a matter of memory licks  :P

JRS

  • Guest
Re: function pointers and memory leacks
« Reply #8 on: July 01, 2014, 04:33:16 PM »
SB hangs on to allocated memory for reuse and releases everything in the end. You can UNDEF a variable to release the memory assigned. Memory / string management is an art in itself.

Q. How much memory has to leak before becoming a pool?


Mike Lobanovsky

  • Guest
Re: function pointers and memory leacks
« Reply #9 on: July 01, 2014, 04:56:44 PM »
A leak in Brussels:



And finally, a cheerful lick:




;D

pber

  • Guest
Re: function pointers and memory leacks
« Reply #10 on: July 01, 2014, 05:02:26 PM »
...this way the pool will be filled slowly, but certainly.

hi Mike

Mike Lobanovsky

  • Guest
Re: function pointers and memory leacks
« Reply #11 on: July 01, 2014, 05:22:54 PM »
Hi Paolo!

A soup of leeks, potatoes and celery and I feel like a king.
Does cipolla zuppa contain potato and/or celery? I always thought it contains only onions and water. :D

SB hangs on to allocated memory for reuse and releases everything in the end. You can UNDEF a variable to release the memory assigned. Memory / string management is an art in itself.
Not only in the end, John. If you're using its sources embedded in, say, a C program, you can also allocate and free (i.e. manage) entire thread memory pools using the API that Peter provided for that purpose.

Quote
Q. How much memory has to leak before becoming a pool?
A. As you can see, sometimes pools are provided to avoid leaks. :)

pber

  • Guest
Re: function pointers and memory leacks
« Reply #12 on: July 01, 2014, 05:45:52 PM »
leeks are not onions!
Mike: the taste is similar,
but color and form ... :-[
...I want to taste white russia cooking, but not too soon  ::)

Mike Lobanovsky

  • Guest
Re: function pointers and memory leacks
« Reply #13 on: July 01, 2014, 08:07:57 PM »


Click here for more!  ;D

(Belarus is neither "white russia" nor "weiß russland". This is all Muscovite propaganda. Belarusians were around long before Moscow was built by the offsprings of the Mongol hordes. Belarusian princes were the backbone of the Grand Duchy of Lithuania and the Magdeburg Law - Europe's first constitution ever - was written in the Belarusian language)

JRS

  • Guest
Re: function pointers and memory leacks
« Reply #14 on: July 01, 2014, 08:36:50 PM »
Damn, that looks good!

You could take on Bubba Burgers with Belarus Bowls.  ;D