Skip to main content
GameDev.net gamedev.net
🔒 Locked

Instances of class disappear.. (pointer problem?)

Started by Bear777 Jul 5, 2008 at 4:00 PM 2 replies 1.2k views
Original Post
Bear777
Bear777
In short, I have a pointer problem. I make instances of a class in function 1, and when i point to those instances in function 2 the program crashes, while executing function 2 inside function 1 doesn't result in a crash. I think this has more to do with my lack of experience regarding C++ than a typical mistake.. but anwyay, the source code is of an open source planetary engine I'm working on, which I'm now redesigning to make it more suitable for the opensource community. See planet.h where it goes wrong, I commented it out. this is the link [Edited by - Bear777 on July 5, 2008 4:28:40 PM]
67rtyus
67rtyus
" In short, I have a pointer problem. I make instances of a class in function 1, and when i point to those instances in function 2 the program crashes, while executing function 2 inside function 1 doesn't result in a crash. "

What comes to my mind just after reading those lines is,that you are instancing the classes in the local scope of the function1, and when function1 returns, the instances disappear from the memory, so the pointers in the function2 point to locations, which are containing some kind of nonsense and eventually lead to crash. When you call function2 in function1, the stack frame of the function1 is still there, and the pointers will work without any problem.

If you are instancing the classes in the function1 like that:

function1()
{
//some code...

classA A;
classB B;

//some other code...
}

try to change your code like that:

function1()
{
//some code...

classA* pA=new A;
classB* pB=new B;

//some other code...
}

The first code will create the class instances in the stack frame of the function1, these instances will be discarded after the function returns. The second code will create them on the heap, they won't be discarded unless you explicitly get rid of them with delete keyword. (like: delete pA, delete pB) Be sure to delete these after you used them, or you will run into memory leaks, which is the one of the most evil things on the mother earth. Try to match every "new" with a "delete".
Hope that helps.
dmatter
dmatter
Quote:
Original post by Bear777
http://www.rocketsoft.gm-school.uni.cc/uploads/openPlanetary.rar << This is the link
That's an address, this is the link. [smile]
Bear777
Bear777
Quote:
Original post by 67rtyus
" In short, I have a pointer problem. I make instances of a class in function 1, and when i point to those instances in function 2 the program crashes, while executing function 2 inside function 1 doesn't result in a crash. "

What comes to my mind
...
Hope that helps.


I am already doing it that way.

Quote:
Original post by dmatter
Quote:
Original post by Bear777
http://www.rocketsoft.gm-school.uni.cc/uploads/openPlanetary.rar << This is the link
That's an address, this is the link. [smile]


Thanks, I use forums which automatically make links too much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.