forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with Static Variables

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue Jul 28, 2009 2:08 am    Post subject: Problem with Static Variables Reply with quote

i'm using c++ and when i declare a static variable in a class it sometimes looses its values.
most of the time when it happens when i'm using it to store a pointer and it crashes the psp most of the ime because i check to see if it is null and it isnt but it doesn't point to the right object anyone have any ideas why because this screw up singelton
Back to top
View user's profile Send private message MSN Messenger
m0skit0



Joined: 02 Jun 2009
Posts: 226

PostPosted: Tue Jul 28, 2009 2:42 am    Post subject: Reply with quote

Maybe the compiler is optimizing the code and that messes your algorithm. Tried "volatile" keyword?
_________________
The Incredible Bill Gates wrote:
The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
Back to top
View user's profile Send private message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue Jul 28, 2009 2:49 am    Post subject: Reply with quote

couldn't use volatile with a static variable

Code:
Icon.h
class Icon
{
     pulbic:
             Icon();
             static Text * iconText;
}

Icon.cpp
Text * Icon::iconText = NULL;

main.cpp
Icon::iconText = new Text(blah,blah);
while(true)
{
uses text.
 works for a while then stops randomly for no reason and crashes because pointer is not valied for some reason
crashes because the pointer is not null and it is not valid
}
Back to top
View user's profile Send private message MSN Messenger
m0skit0



Joined: 02 Jun 2009
Posts: 226

PostPosted: Tue Jul 28, 2009 3:06 am    Post subject: Reply with quote

You shouldn't declare an attribute as public, that violates encapsulation. I guess you're messing with the pointer and that's the result. Better declare the iconText attribute as private and access it through a method. Make sure this method manipulates rightly the attribute.

Anyway, I dont see why would you declare an attribute static. That's usually done for function's local variables, so they keep the value between calls.
_________________
The Incredible Bill Gates wrote:
The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
Back to top
View user's profile Send private message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue Jul 28, 2009 6:30 am    Post subject: Reply with quote

in this instance i declaired iconText static because i had a collection of icons and each would tell the text to change its text and center it. All icons were to use the same text i have since fixed this and given each a local variable pointing to this text. As for the singleton issue it still crashes and the _instance pointer is/was already private
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group