Re: delete_routine() - malfunction or misunderstanding?

new topic     » goto parent     » topic index » view thread      » older message » newer message
ghaberek said...

Perhaps I'm missing something on how delete_routine() is supposed to work.

manual said...

The second way for the delete routine to be called is when its reference count is reduced to 0. Before its memory is freed, the delete routine is called. A default delete will be used if the cleanup parameter to one of the allocate routines is true.

I'm confused here: does this mean I can only associate a delete routine with a memory address? It would seem from the above mention of sequences and atoms, that it can be any object.

That's just some poor writing. It's attempting to point out that the allocate routines have an option to have the underlying memory be deleted when the value returned from the allocation routine's reference count drops to zero.

ghaberek said...

I also found the gem below hidden away in std/machine.e (line 320), which doesn't seem to make it into the manual (see: Type Sorted Function List) because of the extra - on the first line.

---**** 
-- ==== Using non-Memory Objects 
-- 
-- Use call the ##new## routine belonging to the module and you normally don't need to  
-- free it.  You may use [[:delete]] to call its destructors explicitly.  It will be freeed  
-- automatically when the variable becomes unused.  For creating new objects 
-- you can associate a destructor with [[:delete_routine]]. 
-- 

And so, after reading all this, I have the code below. It does not work as I would expect. My delete_obj() routine is being called immediately when I call delete_routine(), and I would expect it to be called later when the object's reference count is reduced to 0 and/or when the application exits and performs its cleanup.

You are not assigning the value returned from delete_routine(). So what happens is that its reference count is immediately decremented. Your code should look like:

obj = delete_routine( obj, delete_rid ) 
 
-- or: 
 
return delete_routine( obj, delete_rid ) 

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu