>
> On Wed, Feb 4, 2009 at 10:03 PM, Phil Malin <
[hidden email]> wrote:
>> Hi all.
>>
>> I've got a weird problem that I'm hoping someone's seen before and has some
>> helpful suggestions.
>>
>> I'm writing a program which links to a C++ library (FLTK). The problem has
>> to do with the destruction of referenced C++ objects - basically I get a
>> 'Received signal 11' error (segmentation violation) when it tries to delete
>> the C++ object (I've traced it to the actual delete call). I have no
>> problem with creating the C++ object and accessing methods on it, only when
>> it comes to deleting it. I wondered if it had to do with me casting the
>> void* to the appropriate type but since accessing other methods work and
>> they do the same thing re casting, I can't see how this is the problem
>> (given my basic knowledge of C++).
If you cast your pointer to/from void*, you should always make sure you do the
same casts, in reverse order. This is a common mistake, and it especially
doesn't work when dealing with multiple inheritance. (ie, even if B inherits
from A, dont do the following : (A*)((void*)(new B())).
Also, make sure you don't delete it twice.
Deleting void* is undefined behaviour.
Deleting anything that has not been created by new is undefined behaviour.
> Things that I think may be related:
> are you using "delete" instead of "delete[]" ?
new -> delete
new[] -> delete[]
> have you got any other memory usage problems? compiling with -g and
> using tools like valgrind or gdb may help.
Compiling with -Wall -Wextra -pedantic might also give some hints (although,
since fltk is broken by design in some regards, it may give too much
information)
Regards,
Julien