Re: memory trampling
On Oct 21, 3:27 pm, joshuajnoble <joshuajno...@gmail.com> wrote:
On Oct 20, 12:03 pm, Zeppe
[...]
I threw together a quick test here, I'm thinking my problem is
in what I'm doing with the static pointer (perhaps). Here's my
Singleton:
This isn't the actual code, since it won't compile...
#ifndef SINGLETON
#define SINGLETON
class SingletonImpl
{
public:
static SingletonImpl* getInst();
int val1;
char* val2[5];
protected:
static SingletonImpl* inst;
SingletonImpl();
};
#endif
SingletonImpl::SingletonImpl(){
val1 = 1;
val2 = "abcdefghijklmn";
since this statement is illegal.
}
SingletonImpl* SingletonImpl::getInst(){
if(inst == 0) {
inst = new SingletonImpl();
}
return inst;
}
And then I have some objects that I need to have access the
singleton in their constructors:
#include "TestObjs.h"
TestObjs::TestObjs(char* ch){
c = ch;
SingletonImpl::getInst()->val2[0] = c;
}
class TestObjs {
public:
TestObjs(char* ch);
char* c;
};
And then my main:
int main (int argc, char * const argv[]) {
char* c = "ghu";
TestObjs t = TestObjs(c);
char* c3 = "3333333";
TestObjs t2 = TestObjs(c3);
return 0;
}
There's also the problem that you never defined
SingletonImpl::inst. Which means undefined behavior---on my
machine, it won't link.
And this blows up...
It doesn't compile, so it can't blow up. What else did you
change when posting, which might be hiding the problem? (The
basic singleton management seems perfectly correct, modulo the
missing data definition. So the problem has to do with
something you've accidentally changed, in the same way you
accidentally changed something which made it illegal, or with
the undefined behavior because of the missing definition of
SingletonImpl::impl---maybe your compiler provides it
implicitly, but doesn't zero initialize it.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34