| View previous topic :: View next topic |
| Author |
Message |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Tue Jul 28, 2009 2:08 am Post subject: Problem with Static Variables |
|
|
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 |
|
 |
m0skit0
Joined: 02 Jun 2009 Posts: 226
|
Posted: Tue Jul 28, 2009 2:42 am Post subject: |
|
|
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 |
|
 |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Tue Jul 28, 2009 2:49 am Post subject: |
|
|
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 |
|
 |
m0skit0
Joined: 02 Jun 2009 Posts: 226
|
Posted: Tue Jul 28, 2009 3:06 am Post subject: |
|
|
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 |
|
 |
coolkehon
Joined: 20 Oct 2008 Posts: 355
|
Posted: Tue Jul 28, 2009 6:30 am Post subject: |
|
|
| 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 |
|
 |
|