This is the mail archive of the ecos-discuss@sourceware.org mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Mixed use of delete with malloc in fclose


Guenter Ebermann wrote:
> Hi All,
> 
> During searching a bug in our firmware (which was not related to ecos)
> I stumbled accross this minor inconsitency (which does not have an effect
> in our setup because the operator delete in libsupc++ of our gcc uses
> free internally):
> 
> In packages/language/c/libc/stdio/current/src/common/fopen.cxx
> line 172 (in Revision 1.9 - HEAD) memory is alloced with malloc and object is
> constructed with placement new:
> 
>     // Allocate it some memory and construct it.
>     curr_stream = (Cyg_StdioStream *)malloc(sizeof(*curr_stream));
>     if (curr_stream == NULL) {
>         cyg_stdio_close( dev );
>         Cyg_libc_stdio_files::unlock();
>         errno = ENOMEM;
>         return NULL;
>     } // if
> 
>     curr_stream = new ((void *)curr_stream) Cyg_StdioStream( dev, open_mode,
>                                                              append, binary,
>                                                              bufmode, bufsize );
> 
> But fclose frees memory useing delete (if it not overwritten with empty stubs).
> Please not that the (void *) cast at the new statement is also useless.
> For a proposed patch please see the attachment.

I'm not sure I understand the issue. If CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS
is not defined, then the implementation of delete in libsupc++ should call
free, just like you say is relevant for your setup.

The fact the constructor was called by placement new should be irrelevant.

Although I guess you could say the call to delete is redundant - you could
always run the code in the CYGFUN_INFRA_EMPTY_DELETE_FUNCTIONS block safely.

So just so I'm sure I understand it, this patch is not fixing a problem,
it's just a bit of cleanup, right?

When you reply you can provide a ChangeLog entry, thanks :-).

Jifl
-- 
eCosCentric Limited      http://www.eCosCentric.com/     The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK.       Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["Si fractum non sit, noli id reficere"]------       Opinions==mine

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]