Re: More memory bugs


Subject: Re: More memory bugs
From: Joaquín Cuenca Abela (cuenca@celium.net)
Date: Mon Mar 19 2001 - 14:39:54 CST


Dom Lachowicz wrote:
>
> This is already done by FREEP, DELETEP, DELETPV in ut_types.h.
>
> #define FREEP(p) do {if(p) {free(p); (p) = NULL;} while (0)

Dom, I think that you need some sleep :)
if we had:

#define FREEP(p) do {if(p) {free(p); (p) = NULL;} while (0)

we will have two opening { and one closing }.
I rechecked it, and I'm seeing (copied & pasted from emacs, after a cvs
update):

#define FREEP(p) do { if (p) free((void *)p); (p)=NULL; } while (0)
#define DELETEP(p) do { if (p) delete(p); (p)=NULL; } while (0)
#define DELETEPV(pa) do { if (pa) delete [] (p); (p)=NULL; }
while (0)

instead of (what I suggest):

#define FREEP(p) do { if (p) { free((void *)p); (p)=NULL; } } while (0)
#define DELETEP(p) do { if (p) { delete(p); (p)=NULL; } } while (0)
#define DELETEPV(pa) do { if (pa) { delete [] (pa);
(pa)=NULL; } } while (0)

(note that your DELETEPV(pa) #define has a bug: you use if (pa), but
delete[] (p) !)

> >I would go for #1. Anyway sometimes we will be still forced to use free
> >(for instance, if Gtk+ gives us a pointer and states that we should free
> >it, we should use g_free and no delete).
>
> Oh, yes, sometimes we will still be forced to free() something that was
> allocated by some other external C library. My point is basically just that
> we shouldn't use (m|c)alloc() ourselves in C++ code, but new instead.

of course, I agree 100%.

Cheers,

--
Joaquín Cuenca Abela
cuenca@celium.net



This archive was generated by hypermail 2b25 : Mon Mar 19 2001 - 14:40:10 CST