[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MFC, Pointers and References

Posted on 2004-11-26
5
Medium Priority
?
332 Views
Last Modified: 2010-04-17
Hello,
Suppose I have two pointers in two different functions and I have a public variable, lets say:
sh1(){
Shape *sh1 = new Shape;
global_sh = sh1;
}

sh2(){
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?

Saqib
0
Comment
Question by:jhapak
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:Skonen
ID: 12687453
Shape *global_sh;

// ....

sh1(){
Shape *sh1 = new Shape;
global_sh = &sh1;
}

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

// ...

Shape *sh3;

sh1();
sh3 = global_sh;
sh3->Something();
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 12689229
Skongen:

> 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;

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

If you call

sh1();
sh2();

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)
0
 
LVL 4

Expert Comment

by:Skonen
ID: 12691613
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.
0
 
LVL 1

Author Comment

by:jhapak
ID: 12714226
ok,
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?
0
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 200 total points
ID: 12716373
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

872 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