|
From: Melchior F. <mf...@kd...> - 2003-08-04 12:41:24
|
In valgrind from CVS/HEAD of today, I get several error messages
like this:
==28293== Mismatched free() / delete / delete []
==28293== at 0x2AAD3C81: __builtin_delete (vg_replace_malloc.c:233)
==28293== by 0x2AAD3C9F: operator delete(void*) (vg_replace_malloc.c:242)
==28293== by 0x83681D3: SkyArchive::_Load(_IO_FILE*) (SkyArchive.cpp:1217)
[...]
==28293== Address 0x52AFACF4 is 0 bytes inside a block of size 27 alloc'd
==28293== at 0x2AAD3B27: __builtin_vec_new (vg_replace_malloc.c:197)
==28293== by 0x2AAD3B7E: operator new[](unsigned) (vg_replace_malloc.c:210)
==28293== by 0x8368165: SkyArchive::_Load(_IO_FILE*) (SkyArchive.cpp:1209)
The concerned code in this case is (without the line numbers):
1209: void* pData = new unsigned char[embeddedItem.iDataSize];
...
1217: delete[] pData;
If I replace the silly "void *" by "unsigned char" then the mismatch
is not reported. (This isn't my code!) The new and delete are used
correctly, only the allocated memory is assigned to a void *. Does
this break the necessary information for gcc to use delete correctly?
Shouldn't the compiler know better, or is it a valgrind problem?
m.
Linux 2.4.20, i386
gcc 3.3pre (SuSE)
libc 2.3.2
|
|
From: Zeljko V. <zel...@av...> - 2003-08-04 13:41:18
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Mon, Aug 04, 2003 at 02:39:15PM +0200, Melchior FRANZ wrote: > > The concerned code in this case is (without the line numbers): > > 1209: void* pData = new unsigned char[embeddedItem.iDataSize]; > ... > 1217: delete[] pData; > > > If I replace the silly "void *" by "unsigned char" then the mismatch > is not reported. (This isn't my code!) The new and delete are used > correctly, only the allocated memory is assigned to a void *. Does > this break the necessary information for gcc to use delete correctly? > Shouldn't the compiler know better, or is it a valgrind problem? > You can't use either delete nor delete[] on void* since the compiler must know the exact type in order to call destructor for the object(s) (delete MUST call destructors). I don't know the relevant part of the standard, but it's just common sense... :) So valgrind is not wrong, your code is. - -- v Zeljko Vrba, dipl. ing. http://pgp.mit.edu:11371/pks/lookup?op=vindex&search=0x3B375F1C zel...@av... Advanced Simulation Technologies http://www.avl.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE/LmFAzT2jbzs3XxwRAvakAKCtxbjNaMUFJ5sbclep58JuXbCCSwCfUPpp oafI0uSrxT7gmsL5WwTNSIA= =JKk2 -----END PGP SIGNATURE----- |
|
From: Melchior F. <mf...@kd...> - 2003-08-04 13:51:03
|
* Zeljko Vrba -- Monday 04 August 2003 15:36: > On Mon, Aug 04, 2003 at 02:39:15PM +0200, Melchior FRANZ wrote: > > 1209: void* pData = new unsigned char[embeddedItem.iDataSize]; > > ... > > 1217: delete[] pData; > You can't use either delete nor delete[] on void* since the compiler must > know the exact type in order to call destructor for the object(s) Well, the compiler does very well know the type. It was the one who allocated it, remember? :-} I wasn't sure about this, and Stroustrup didn't tell me. > So valgrind is not wrong, your code is. It isn't =my= code. I'm just debugging someone else's. m. PS: Please don't CC to me. I'm subscribed! |
|
From: Tom H. <th...@cy...> - 2003-08-04 14:15:44
|
In message <200...@pf...>
Melchior FRANZ <mf...@kd...> wrote:
> * Zeljko Vrba -- Monday 04 August 2003 15:36:
>> On Mon, Aug 04, 2003 at 02:39:15PM +0200, Melchior FRANZ wrote:
>> > 1209: void* pData = new unsigned char[embeddedItem.iDataSize];
>> > ...
>> > 1217: delete[] pData;
>
>> You can't use either delete nor delete[] on void* since the compiler must
>> know the exact type in order to call destructor for the object(s)
>
> Well, the compiler does very well know the type. It was the one
> who allocated it, remember? :-}
Well it does in that case, yes. But you might be deleting it in
a different source file from the one where it was allocated and
then the compiler wouldn't have a hope of knowing!
Tom
--
Tom Hughes (th...@cy...)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
|
|
From: Melchior F. <mf...@kd...> - 2003-08-04 14:29:18
|
* Tom Hughes -- Monday 04 August 2003 16:14: > Well it does in that case, yes. But you might be deleting it in > a different source file from the one where it was allocated and > then the compiler wouldn't have a hope of knowing! True. And expecting the compiler to store the type of allocation for cases like this is asked a bit too much, even it it could do so. After all, this construct hasn't any merits anyway. I'll fix this in that code, which is =not= =my= code. :-) m. |