[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pointer question



Ted Graves wrote:
> 
> Hi all,
> 
> Another lurker question ... let's say you define a pointer using the PTR_NEW
> function and assign to a variable x.  As long as you keep track of x and don't
> reassign x and lose the pointer to the heap variable, things are great.  You
> can remove the heap variable from memory using the PTR_FREE procedure.
> 
> But now let's say i have a function TEST that takes a pointer as an argument,
> and i want to create a pointer on the fly to use in TEST.  So i do something
> like
> 
> result = TEST(PTR_NEW(value))
> 
> where value is whatever i want the heap variable to be. What happens to the
> heap variable assigned in this statement after TEST returns?  I'm assuming
> from that because of the way it was created, a heap variable now exists that i
> can't easily get rid of without using HEAP_GC.
> 
> Me and my sloppy programming ...

On an only slightly related note, does everyone know that you can recover a
pointer to a "lost" heap variable using ptr_valid?  Here's an example:

IDL> a=ptr_new(1)
IDL> print,a
<PtrHeapVar4>
IDL> a='oh no, I overwrote my pointer variable'
IDL> help,/heap_variables
Heap Variables:
    # Pointer: 1
    # Object : 0

<PtrHeapVar4>   INT       =        1
IDL> a=ptr_valid(4,/CAST)
IDL> print,*a
       1

You can also get a vector of pointers to every heap variable using:

IDL> pvec=ptr_valid()

While this isn't exactly useful programatically, it may get you out of a pinch.

JD