Problem with inheritance and object share use?
This is the skeleton of my app.
Code:
- ClassA has a ClassE * myclassE_inside_A public var.
- ClassB Inherits ClassD
- ClassC Inherits ClassD
- ClassD has some funtions and *ClassE public var.
The idea was to deal with classE instance from class A, B and C. And use some common methods of ClassD from ClassB and ClassC.
Pseudocode that describes its operation.
Code:
Inside ClassA:
ClassB my_classB;
my_classB.create; (I have the *ClassE filled with data )
myclassE_inside_A = my_classB.classE;
ClassC my_classC;
ClassC.set_classE(my_classE_insideA);
for( )
{ do some things with my_classE_inside_A ;
classC.modify_some_things_on_classE }
- classC.modify_some_things_on_classE uses own methods and one function inheritated from classD.
When I run the program I have a lot a heap error and a lot of crahes
HEAP: Free Heap block c977e28 modified at c977e5c after it was freed
(Internal error: pc 0x2ff in read in psymtab, but not in symtab.)
(Internal error: pc 0x2ff in read in psymtab, but not in symtab.)
(Internal error: pc 0x2ff in read in psymtab, but not in symtab.)
,,,,
The error was related with classC, after the first modification of classE object. the second time I use it (it is the same if I do it from myclassE_insideA or int classC) i have the crash.
I'm sure that I' forget to do something but I dont view it.
It is as If I can't use the classE object by pointer. ?
Any idea ?
Re: Problem with inheritance and object share use?
I dont like this idea - who is responsible for deleting the classE object ?
Code:
ClassB my_classB;
my_classB.create; (I have the *ClassE filled with data )
myclassE_inside_A = my_classB.classE; // who owns the classE now ? this or my_classB
ClassC my_classC;
ClassC.set_classE(my_classE_insideA); // and now ? is ownership passed to my_classC ?
You can get lost here easily, passing around, modifying and storing pointer to an object created by temporary object (my_classB on stack) smells like crash sooner or later.
Quote:
Free Heap block c977e28 modified at c977e5c after it was freed
I dont know who owns the created object, and it seems like you dont know either, because you want to access it after deleting it somewhere.
I gues you should rethink the design, because its not clear now, and will be very painful to modify and maintain this kind of code.
Re: Problem with inheritance and object share use?
One of possible solutions is to do reference counting on "E" and copy it by value (with a shared reference counter of course) between those objects that use it. Or if you use Qt, QSharedPointer is the thing you want to use (if you don't but you use C++ you can use boost/std::shared_ptr). And for clarity E should be created outside B to avoid doubts related to ownership of E (since E can live longer than B if it is shared with another object). As for now you are trying to access an already deleted object.