MFC, Pointers and References

Posted on 2004-11-26
Last Modified: 2010-04-17
Suppose I have two pointers in two different functions and I have a public variable, lets say:
Shape *sh1 = new Shape;
global_sh = sh1;

Shape *sh2 = new Shape;
global_sh = sh2;

What I want is something to store the address of the point sh1 or sh2 in global_sh. Now somewhere else I declare another pointer "sh3" and say sh3 = global_sh. Now if I say sh3.something() then it should return/call the related object. How can i do it?

Question by:jhapak
    LVL 4

    Expert Comment

    Shape *global_sh;

    // ....

    Shape *sh1 = new Shape;
    global_sh = &sh1;

    sh2() {
    Shape *sh2 = new Shape;
    global_sh = &sh2;

    // ...

    Shape *sh3;

    sh3 = global_sh;
    LVL 7

    Expert Comment


    > Shape *sh1 = new Shape;
    > global_sh = &sh1;

    that's bad, since you take the address of a temporary object. looking at the calling code you probably meant

    global_sh = sh1;

    With above correction, this would work fine.
    However, you have another problem: who is going to delete the shapes?

    If you call


    at least the first allocation leaks, because it is not accessible anymore.
    To solve that, I'd recommmend using reference counted pointers (e.g. boost::shared_ptr)
    LVL 4

    Expert Comment

    Sorry about that, looking back at that I see no reasoning in what I did. I assumed he was going to delete global_sh afterwards so I didn't mention anything about freeing the allocated memory once he was done.
    LVL 1

    Author Comment

    I have checked smart pointers, cool thing.

    But, my problem is that am using CObList and I have different classes in it. So when ever I put an object on the list it gives me memory leak. Any idea?? How can I delete manually a pointer then?
    LVL 7

    Accepted Solution

    Wit h raw pointers, it very much depends on the flow of your *entire* program, how and if you can avoid memory leaks. Besides such a discussion is beyond EE (since we would have to analyze your entire prog), I would strongly disfavor it as a huge pitfall when any change i introduced.

    My *recommendation* is to:

    - use a typedef boost:shared_ptr<CObject> CObjectPtr
    - use a CList<CObjectPtr> or a std::list<CObjectPtr> instead of th CObList
    - use a CObjectPtr for the global variable.

    Note: this is just an outline, I don't know if there are specific features of CObList that you would lose this way. It might be a little bit mroe work to begin with. But, "smart pointers have changed my life", and I don't want to go back.

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    count8 challlenge 13 68
    countHi challenge 25 61
    changeXy challenge 13 40
    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This article will show, step by step, how to integrate R code into a R Sweave document
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now