Results 1 to 3 of 3

Thread: Problem with inheritance and object share use?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with inheritance and object share use?

    This is the skeleton of my app.
    Qt Code:
    1. - ClassA has a ClassE * myclassE_inside_A public var.
    2. - ClassB Inherits ClassD
    3. - ClassC Inherits ClassD
    4.  
    5. - ClassD has some funtions and *ClassE public var.
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. Inside ClassA:
    2. ClassB my_classB;
    3. my_classB.create; (I have the *ClassE filled with data )
    4. myclassE_inside_A = my_classB.classE;
    5.  
    6. ClassC my_classC;
    7. ClassC.set_classE(my_classE_insideA);
    8. for( )
    9. { do some things with my_classE_inside_A ;
    10. classC.modify_some_things_on_classE }
    11.  
    12. - classC.modify_some_things_on_classE uses own methods and one function inheritated from classD.
    To copy to clipboard, switch view to plain text mode 

    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 ?
    Last edited by tonnot; 1st August 2011 at 11:55.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with inheritance and object share use?

    I dont like this idea - who is responsible for deleting the classE object ?
    Qt Code:
    1. ClassB my_classB;
    2. my_classB.create; (I have the *ClassE filled with data )
    3. myclassE_inside_A = my_classB.classE; // who owns the classE now ? this or my_classB
    4.  
    5. ClassC my_classC;
    6. ClassC.set_classE(my_classE_insideA); // and now ? is ownership passed to my_classC ?
    To copy to clipboard, switch view to plain text mode 
    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.
    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    tonnot (2nd August 2011)

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 15:29
  2. Replies: 2
    Last Post: 6th May 2011, 06:56
  3. problem with constructor and inheritance QFile object
    By naturalpsychic in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 10:15
  4. Replies: 4
    Last Post: 20th August 2010, 13:07
  5. Inheritance problem
    By brevleq in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2008, 06:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.