Witam.
Po 2 dniowej walce stwierdzam, iż się poddaje...
Mam 3 pliki (właściwie w projekcie jest ich z 30, ale zajme się opisywaniem tych trzech).
Singleton.h
Objects.cpp
Objects.h
Kod źródłowy Objects.h
#define ADDRESS 0x784D2C
#ifndef __OBJECTS_H
#define __OBJECTS_H
enum ObjectFlags
{
HasHelpLens = 0x20000000,
IsGround = 0x40000000
};
enum ObjectLensHelp
{
HasSpecialDescription = 0x457,
IsReadOnly = 0x458
};
class ObjectProperties
{
public:
int width;
int height;
ObjectFlags flags;
ObjectLensHelp lensHelp;
bool hasFlag(ObjectFlags flag)
{
return (flags & flag) ? true : false;
}
};
class Objects
{
public:
// Konstruktor / Destruktor
Objects();
~Objects();
ObjectProperties* getObjectById(UINT16 itemid);
int heapStart;
};
#endif
Kod źródłowy Objects.cpp
#include "stdafx.h"
#include "objects.h"
extern void debug(const char* napis);
extern void debug(int liczba);
Objects::Objects()
{
if(!Singleton::objects)
Singleton::objects = this;
int output = (*(int*)(ADDRESS)) + 0x08;
this->heapStart = *(int*)(output);
debug("heapStart z konstruktora");
debug(heapStart);
}
Objects::~Objects()
{
Singleton::objects = NULL;
}
ObjectProperties* Objects::getObjectById(UINT16 itemid)
{
debug("Wskazniki");
debug((int)this); // W debugowym pliku drukuje 0... (dlaczego?)
debug((int)objects);
ObjectProperties* object = new ObjectProperties;
// No i tutaj następuje crash... z powodu odwołania się do heapStart
object->width = heapStart;
object->height = heapStart;
debug("Tego już nie wyświetli bo nastąpil crash programu");
return object;
}
Kod źródłowy Singleton.h
#ifndef __SINGLETON_H
#define __SINGLETON_H
class Objects;
class Protocol;
namespace Singleton
{
static Objects* objects = NULL;
static Protocol* protocol = NULL;
};
#endif
Problem w krokach opisalem także w komentarzach w kodzie źródłowym.
Wyjaśnię jeszcze raz:
po prostu w funkcji getObjectById nie ma dostępu do wskaźnika this...
Ma on wartość 0.
Jeżeli bym w funkcji getObjectById odwoływał się do Singleton::objects to wszystko jest ok.
Jeżeli ktoś by dał rade pomóc to bym był bardzo wdzięczny.
Pozdrawiam.