Trying to solve the properties memory problem

This commit is contained in:
Ilya Mizus 2014-04-09 10:16:53 +04:00
parent bade22ff49
commit 6e3b170393
3 changed files with 15 additions and 4 deletions

View File

@ -1263,7 +1263,7 @@ class Machine {
Thread* exclusive;
Thread* finalizeThread;
Reference* jniReferences;
const char** properties;
char** properties;
unsigned propertyCount;
const char** arguments;
unsigned argumentCount;

View File

@ -3940,10 +3940,10 @@ JNI_CreateJavaVM(Machine** m, Thread** t, void* args)
}
unsigned cpl = strlen(classpath);
RUNTIME_ARRAY(char, classpathProperty, cpl + sizeof(CLASSPATH_PROPERTY) + 1);
RUNTIME_ARRAY(char, classpathProperty, cpl + strlen(CLASSPATH_PROPERTY) + 2);
if (addClasspathProperty) {
char* p = RUNTIME_ARRAY_BODY(classpathProperty);
local::append(&p, CLASSPATH_PROPERTY, sizeof(CLASSPATH_PROPERTY), '=');
local::append(&p, CLASSPATH_PROPERTY, strlen(CLASSPATH_PROPERTY), '=');
local::append(&p, classpath, cpl, 0);
*(propertyPointer++) = RUNTIME_ARRAY_BODY(classpathProperty);
}

View File

@ -3119,7 +3119,6 @@ Machine::Machine(System* system, Heap* heap, Finder* bootFinder,
exclusive(0),
finalizeThread(0),
jniReferences(0),
properties(properties),
propertyCount(propertyCount),
arguments(arguments),
argumentCount(argumentCount),
@ -3164,6 +3163,14 @@ Machine::Machine(System* system, Heap* heap, Finder* bootFinder,
if (codeLibraryName && (codeLibraryNameEnd = strchr(codeLibraryName, system->pathSeparator())))
*codeLibraryNameEnd = 0;
// Copying the properties memory (to avoid memory crashes)
this->properties = (char**)heap->allocate(sizeof(char*) * propertyCount);
for (unsigned int i = 0; i < propertyCount; i++)
{
this->properties[i] = (char*)heap->allocate(sizeof(char) * (strlen(properties[i]) + 1));
strcpy(this->properties[i], properties[i]);
}
if (not system->success(system->make(&localThread)) or
not system->success(system->make(&stateLock)) or
not system->success(system->make(&heapLock)) or
@ -3222,6 +3229,10 @@ Machine::dispose()
heap->free(arguments, sizeof(const char*) * argumentCount);
for (unsigned int i = 0; i < propertyCount; i++)
{
heap->free(properties[i], sizeof(char) * (strlen(properties[i]) + 1));
}
heap->free(properties, sizeof(const char*) * propertyCount);
static_cast<HeapClient*>(heapClient)->dispose();