some cleanup and fixing of memory issue

This commit is contained in:
ZappoMan 2013-08-13 11:37:57 -07:00
parent 60dedee739
commit d3ce3e4e60
5 changed files with 81 additions and 68 deletions

View file

@ -3872,8 +3872,13 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng
_voxelFades.push_back(fade);
}
// store jurisdiction details for later use
_voxelServerJurisdictions[nodeID] = JurisdictionMap(_voxelSceneStats.getJurisdictionRoot(),
_voxelSceneStats.getJurisdictionEndNodes());
// This is bit of fiddling is because JurisdictionMap assumes it is the owner of the values used to construct it
// but VoxelSceneStats thinks it's just returning a reference to it's contents. So we need to make a copy of the
// details from the VoxelSceneStats to construct the JurisdictionMap
JurisdictionMap jurisdictionMap;
jurisdictionMap.copyContents(_voxelSceneStats.getJurisdictionRoot(), _voxelSceneStats.getJurisdictionEndNodes());
_voxelServerJurisdictions[nodeID] = jurisdictionMap;
}
return statsMessageLength;
}

View file

@ -319,3 +319,48 @@ bool isAncestorOf(unsigned char* possibleAncestor, unsigned char* possibleDescen
// they all match, so we are an ancestor
return true;
}
unsigned char* hexStringToOctalCode(const QString& input) {
const int HEX_NUMBER_BASE = 16;
const int HEX_BYTE_SIZE = 2;
int stringIndex = 0;
int byteArrayIndex = 0;
// allocate byte array based on half of string length
unsigned char* bytes = new unsigned char[(input.length()) / HEX_BYTE_SIZE];
// loop through the string - 2 bytes at a time converting
// it to decimal equivalent and store in byte array
bool ok;
while (stringIndex < input.length()) {
uint value = input.mid(stringIndex, HEX_BYTE_SIZE).toUInt(&ok, HEX_NUMBER_BASE);
if (!ok) {
break;
}
bytes[byteArrayIndex] = (unsigned char)value;
stringIndex += HEX_BYTE_SIZE;
byteArrayIndex++;
}
// something went wrong
if (!ok) {
delete[] bytes;
return NULL;
}
return bytes;
}
QString octalCodeToHexString(unsigned char* octalCode) {
const int HEX_NUMBER_BASE = 16;
const int HEX_BYTE_SIZE = 2;
QString output;
if (!octalCode) {
output = "00";
} else {
for (int i = 0; i < bytesRequiredForCodeLength(*octalCode); i++) {
output.append(QString("%1").arg(octalCode[i], HEX_BYTE_SIZE, HEX_NUMBER_BASE, QChar('0')).toUpper());
}
}
return output;
}

View file

@ -10,6 +10,7 @@
#define __hifi__OctalCode__
#include <string.h>
#include <QString>
const int BITS_IN_BYTE = 8;
const int BITS_IN_OCTAL = 3;
@ -49,4 +50,8 @@ typedef enum {
} OctalCodeComparison;
OctalCodeComparison compareOctalCodes(unsigned char* code1, unsigned char* code2);
QString octalCodeToHexString(unsigned char* octalCode);
unsigned char* hexStringToOctalCode(const QString& input);
#endif /* defined(__hifi__OctalCode__) */

View file

@ -18,7 +18,7 @@
// copy assignment
JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) {
copyContents(other);
printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap const& other) COPY ASSIGNMENT %p from %p\n", this, &other);
//printf("JurisdictionMap COPY ASSIGNMENT %p from %p\n", this, &other);
return *this;
}
@ -27,7 +27,7 @@ JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) {
init(other._rootOctalCode, other._endNodes);
other._rootOctalCode = NULL;
other._endNodes.clear();
printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) MOVE ASSIGNMENT %p from %p\n", this, &other);
//printf("JurisdictionMap MOVE ASSIGNMENT %p from %p\n", this, &other);
return *this;
}
@ -36,41 +36,45 @@ JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL)
init(other._rootOctalCode, other._endNodes);
other._rootOctalCode = NULL;
other._endNodes.clear();
printf("JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) MOVE CONSTRUCTOR %p from %p\n", this, &other);
//printf("JurisdictionMap MOVE CONSTRUCTOR %p from %p\n", this, &other);
}
// Copy constructor
JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) {
copyContents(other);
printf("JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) COPY CONSTRUCTOR %p from %p\n", this, &other);
//printf("JurisdictionMap COPY CONSTRUCTOR %p from %p\n", this, &other);
}
void JurisdictionMap::copyContents(const JurisdictionMap& other) {
void JurisdictionMap::copyContents(unsigned char* rootCodeIn, const std::vector<unsigned char*> endNodesIn) {
unsigned char* rootCode;
std::vector<unsigned char*> endNodes;
if (other._rootOctalCode) {
int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._rootOctalCode));
if (rootCodeIn) {
int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(rootCodeIn));
rootCode = new unsigned char[bytes];
memcpy(rootCode, other._rootOctalCode, bytes);
memcpy(rootCode, rootCodeIn, bytes);
} else {
rootCode = new unsigned char[1];
*rootCode = 0;
}
for (int i = 0; i < other._endNodes.size(); i++) {
if (other._endNodes[i]) {
int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._endNodes[i]));
for (int i = 0; i < endNodesIn.size(); i++) {
if (endNodesIn[i]) {
int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodesIn[i]));
unsigned char* endNodeCode = new unsigned char[bytes];
memcpy(endNodeCode, other._endNodes[i], bytes);
memcpy(endNodeCode, endNodesIn[i], bytes);
endNodes.push_back(endNodeCode);
}
}
init(rootCode, endNodes);
}
void JurisdictionMap::copyContents(const JurisdictionMap& other) {
copyContents(other._rootOctalCode, other._endNodes);
}
JurisdictionMap::~JurisdictionMap() {
clear();
printf("JurisdictionMap::~JurisdictionMap() DESTRUCTOR %p\n",this);
//printf("JurisdictionMap DESTRUCTOR %p\n",this);
}
void JurisdictionMap::clear() {
@ -93,19 +97,19 @@ JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) {
std::vector<unsigned char*> emptyEndNodes;
init(rootCode, emptyEndNodes);
printf("JurisdictionMap::~JurisdictionMap() DEFAULT CONSTRUCTOR %p\n",this);
//printf("JurisdictionMap DEFAULT CONSTRUCTOR %p\n",this);
}
JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) {
clear(); // clean up our own memory
readFromFile(filename);
printf("JurisdictionMap::~JurisdictionMap() INI FILE CONSTRUCTOR %p\n",this);
//printf("JurisdictionMap INI FILE CONSTRUCTOR %p\n",this);
}
JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes)
: _rootOctalCode(NULL) {
init(rootOctalCode, endNodes);
printf("JurisdictionMap::~JurisdictionMap() OCTCODE CONSTRUCTOR %p\n",this);
//printf("JurisdictionMap OCTCODE CONSTRUCTOR %p\n",this);
}
JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHexCodes) {
@ -122,7 +126,7 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe
//printOctalCode(endNodeOctcode);
_endNodes.push_back(endNodeOctcode);
}
printf("JurisdictionMap::~JurisdictionMap() HEX STRING CONSTRUCTOR %p\n",this);
//printf("JurisdictionMap HEX STRING CONSTRUCTOR %p\n",this);
}
@ -207,48 +211,3 @@ bool JurisdictionMap::writeToFile(const char* filename) {
settings.endGroup();
return true;
}
unsigned char* JurisdictionMap::hexStringToOctalCode(const QString& input) const {
const int HEX_NUMBER_BASE = 16;
const int HEX_BYTE_SIZE = 2;
int stringIndex = 0;
int byteArrayIndex = 0;
// allocate byte array based on half of string length
unsigned char* bytes = new unsigned char[(input.length()) / HEX_BYTE_SIZE];
// loop through the string - 2 bytes at a time converting
// it to decimal equivalent and store in byte array
bool ok;
while (stringIndex < input.length()) {
uint value = input.mid(stringIndex, HEX_BYTE_SIZE).toUInt(&ok, HEX_NUMBER_BASE);
if (!ok) {
break;
}
bytes[byteArrayIndex] = (unsigned char)value;
stringIndex += HEX_BYTE_SIZE;
byteArrayIndex++;
}
// something went wrong
if (!ok) {
delete[] bytes;
return NULL;
}
return bytes;
}
QString JurisdictionMap::octalCodeToHexString(unsigned char* octalCode) const {
const int HEX_NUMBER_BASE = 16;
const int HEX_BYTE_SIZE = 2;
QString output;
if (!octalCode) {
output = "00";
} else {
for (int i = 0; i < bytesRequiredForCodeLength(*octalCode); i++) {
output.append(QString("%1").arg(octalCode[i], HEX_BYTE_SIZE, HEX_NUMBER_BASE, QChar('0')).toUpper());
}
}
return output;
}

View file

@ -43,15 +43,14 @@ public:
unsigned char* getRootOctalCode() const { return _rootOctalCode; }
unsigned char* getEndNodeOctalCode(int index) const { return _endNodes[index]; }
int getEndNodeCount() const { return _endNodes.size(); }
void copyContents(unsigned char* rootCodeIn, const std::vector<unsigned char*> endNodesIn);
private:
void copyContents(const JurisdictionMap& other);
void copyContents(const JurisdictionMap& other); // use assignment instead
void clear();
void init(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes);
unsigned char* hexStringToOctalCode(const QString& input) const;
QString octalCodeToHexString(unsigned char* octalCode) const;
unsigned char* _rootOctalCode;
std::vector<unsigned char*> _endNodes;
};