On Tue, 16 May 2017 14:01:24 -0400, DFS <
nos...@dfs.com> wrote:
>I don't get hung up on scripting vs programming, compiled vs
>interpreted, etc.
Well, you *should* get "hung up" on the differences between languages
because those differences affect using the languages correctly.
Many popular languages now are "managed" - i.e. the runtime
environment actively tries to help the programmer avoid mistakes.
Managed languages usually have automatic "garbage collection" which
frees memory used by objects when the program no longer has any
references (pointers) to them.
You cannot accidentally "lose" an object in a managed language because
you never "free" objects yourself - the runtime GC system watches what
the program does and knows when it is safe to free something.
C and C++ are not managed and they do not automatically free anything.
If you overwrite a pointer to an object, you lose the object if the
pointer was the only one referencing it.
>'when [PGResult object] is no longer needed' sounds like it can be
>cleared once, at the end of the script.
> :
>In VB it's not necessary but I always closed them before the next
>usage.
>
> Set rs = db.openrecordset("SELECT DATA;")
> if rs.recordcount > 0 then
> ... do things
> endif
> rs.close
>
> Set rs = db.openrecordset("SELECT DATA 2nd time;")
> if rs.recordcount > 0 then
> ... do things
> endif
> rs.close
VB has garbage collection. When you set 'rs', the object it
previously referred to is recycled. You could remove the close calls
entirely and the program probably[1] still would not leak memory.
C doesn't have garbage collection[2]. If you lose the result pointer
and don't call PGClear on it, the memory used by the result set is
lost.
There are times when you might want to keep results around for a
while. You might, for instance, pass the results to a separate thread
for processing. But then that thread has to call PGClear on the
pointer when it is done.
George
[1] depends on the 'db' object implementation.
[2] there are some automatic GC libraries for C and C++, which replace
the normal malloc/free, new/delete calls with managed versions. The
most well respected ones are the Boehm-Demers-Weiser collector and its
offshoot TinyGC.
http://www.hboehm.info/gc/
https://github.com/ivmai/tinygc
There are some others by Barlett, Detlefs, etc., including some
copying/moving collectors for C++, but they aren't as well known ...
and they place some (minor) restrictions on how you can code.
For some uses a GC library can work very well, but it isn't the same
as built-in GC where the compiler and runtime environment cooperate.