Re: Proper pointer cleanup question [message #34661 is a reply to message #34659] |
Tue, 08 April 2003 11:03   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
M. Katz (MKatz843@onebox.com) writes:
> Thanks! I'll write myself a full reverse-ordered cleanup routine.
> I suppose this explicit cleanup is just as important for objects as
> well:
> Object pointer fields should be explicitly freed in the Cleanup
> method.
>
> Question 1) But what about simple scalar pointers?
> a = ptr_new(fltarr(10,10))
>
> If I set
> a = 0
> Will I have stranded my fltarr(), or is IDL smart enough to deallocate
> it properly?
You will have leaking memory. IDL, as a weakly typed
language, always allows you to write dangerous code. :-)
> Question 2) Then how about this scenario
> a = ptr_new(fltarr(10,10))
> b = ptr_new(dblarr(5,5))
>
> a = b ;--- Does this strand the original a array?
Yes, for the same reason as above. You are re-defining A
before you have freed the memory the original pointer A
pointed to.
> *a = *b ;--- How about this?
This is perfectly OK. IDL handles pointer memory as it does
regular variable memory. Pointer A now points to the same data
as pointer B, but the memory pointer A originally pointed to
has been de-allocated by IDL.
> From looking at help, /memory and testing the above, I think the
> answer to both of my questions is that memory IS stranded unless you
> explicitly free it in all of the above cases.
No, *ptr = someNewData is always permissible. Just like
this is permissible:
a = 5
a = [10, 10]
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|