I am working on an application for both iOS and Android and I am getting very different results in my C++ code. The issue I am having is that entering a function is stomping a member variable is a derived class. As an example
int main()
{
A** arrayOfA = new A[10];
for (int i = 0; i < 10; ++i)
{
arrayOfA[i] = new D();
}
return 0;
};
class A
{
// Original code here. After this is the added function and member variable that breaks it
public:
virtual u32 GetterFunction() = 0;
protected:
u32 ProblemVariable;
};
class B : A
{
public:
B(u32 index) { ProblemVariable = index }
virtual u32 StompedGetter() = 0;
virtual u32 GetterFunction() { return ProblemVariable; }
void Initialize()
{
u32 sometimesBunkNumber = StompedGetter();
}
protected:
u32 StompedVariable;
};
class C : A
{
public:
virtual u32 GetterFunction() { return 0; }
};
class D : B
{
B() { Initialize(); }
virtual StompedGetter() { return StompedVariable; }
}
class E : B
{
virtual StompedGetter() { return 0; }
}
Now then, the issue here is that everything works fine UNTIL I put the member variable and virtual getter in class A. When the getter is called it gives a bunk number at random times. It might work 2 times and then it will return a bad number (obviously grabbing from a bad place in memory). It doesn't matter if the function is virtual or not but it is the ideal solution. The worst part is that this compiles and works great on the iOS version but on Android it dies. Just as a clarification it runs until it initializes a class B in an array of class A. Sorry if this is very vague but any and all help is appreciated. Thanks in advance!
The only thing I can think of, given the code you've shown, is that the line
A** arrayOfA = new A[10];
should instead be
A** arrayOfA = new A*[10];
The symptoms you describe seem to indicate that you're performing some kind of memory violation, and treating an array of pointers to pointers to A as an array of A's, then accessing those A's later by assigning D's to them, would probably cause that. It probably worked fine before the member variable was added since beforehand it would have just been an empty struct (the size of an address.) Now that sizeof(A) != sizeof(A*), you're stepping all over your memory.
I am working on an application for both iOS and Android and I am getting very different results in my C++ code. The issue I am having is that entering a function is stomping a member variable is a derived class. As an example
int main()
{
A** arrayOfA = new A[10];
for (int i = 0; i < 10; ++i)
{
arrayOfA[i] = new D();
}
return 0;
};
class A
{
// Original code here. After this is the added function and member variable that breaks it
public:
virtual u32 GetterFunction() = 0;
protected:
u32 ProblemVariable;
};
class B : A
{
public:
B(u32 index) { ProblemVariable = index }
virtual u32 StompedGetter() = 0;
virtual u32 GetterFunction() { return ProblemVariable; }
void Initialize()
{
u32 sometimesBunkNumber = StompedGetter();
}
protected:
u32 StompedVariable;
};
class C : A
{
public:
virtual u32 GetterFunction() { return 0; }
};
class D : B
{
B() { Initialize(); }
virtual StompedGetter() { return StompedVariable; }
}
class E : B
{
virtual StompedGetter() { return 0; }
}
Now then, the issue here is that everything works fine UNTIL I put the member variable and virtual getter in class A. When the getter is called it gives a bunk number at random times. It might work 2 times and then it will return a bad number (obviously grabbing from a bad place in memory). It doesn't matter if the function is virtual or not but it is the ideal solution. The worst part is that this compiles and works great on the iOS version but on Android it dies. Just as a clarification it runs until it initializes a class B in an array of class A. Sorry if this is very vague but any and all help is appreciated. Thanks in advance!
The only thing I can think of, given the code you've shown, is that the line
A** arrayOfA = new A[10];
should instead be
A** arrayOfA = new A*[10];
The symptoms you describe seem to indicate that you're performing some kind of memory violation, and treating an array of pointers to pointers to A as an array of A's, then accessing those A's later by assigning D's to them, would probably cause that. It probably worked fine before the member variable was added since beforehand it would have just been an empty struct (the size of an address.) Now that sizeof(A) != sizeof(A*), you're stepping all over your memory.
0 commentaires:
Enregistrer un commentaire