Jump to content
  • Advertisement
Sign in to follow this  
Bear777

Unity Instances of class disappear.. (pointer problem?)

This topic is 3694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
" 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!