--- /tmp/UDF-bug38297-0.0.1dev/bug38297.cc 2008-07-24 01:35:57.000000000 +1000 +++ bug38297.cc 2014-12-21 13:45:55.911961574 +1100 @@ -36,15 +36,15 @@ } +static ofstream logfile; + struct data { unsigned long long count; int id; - ofstream logfile; }; - my_bool udf_count_init( UDF_INIT* initid, UDF_ARGS* args, char* message ) { @@ -60,39 +60,23 @@ return 1; } + try { + logfile.open("/tmp/bug38297.log", ios::out | ios::app | ios::binary); - data *buffer = new data; - if (buffer == NULL) { - strcpy(message, "Cannot allocate memory for data"); - return 1; - } - - buffer->count = 0; - - /* Seed the random-number generator with current time so that - * the numbers will be different every time we run. - */ - srand( (unsigned)time( NULL ) ); - buffer->id = rand(); - - try { - buffer->logfile.open("/tmp/bug38297.log", ios::out | ios::app | ios::binary); - - if (!buffer->logfile.is_open()) { - throw "Can't open logfile for writing"; - } - } catch (char * str) { - strcpy(message, str); - return 1; - } + if (!logfile.is_open()) { + throw "Can't open logfile for writing"; + } + } catch (char * str) { + strcpy(message, str); + return 1; + } initid->maybe_null = 1; initid->max_length = 32; //initid->const_item = 0; - - initid->ptr = (char*)buffer; - - buffer->logfile << "udf_count_init() initid " << initid << " internal id " << buffer->id << endl; + initid->ptr = NULL; + + logfile << "udf_count_init() initid " << initid << endl; return 0; } @@ -104,9 +88,9 @@ data *buffer = (data*)initid->ptr; - if (buffer->logfile.is_open()) { - buffer->logfile << "udf_count_deinit() initid " << initid << " internal id " << buffer->id << " closing logfile" << endl; - buffer->logfile.close(); + if (logfile.is_open()) { + logfile << "udf_count_deinit() initid " << initid << " internal id " << buffer->id << " closing logfile" << endl; + logfile.close(); } // free(buffer); delete buffer; @@ -118,7 +102,7 @@ void udf_count_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* is_error ) { data *buffer = (data*)initid->ptr; - buffer->logfile << "udf_count_reset() " << buffer->id << endl; + logfile << "udf_count_reset() " << buffer->id << endl; udf_count_clear(initid, is_null, is_error); udf_count_add( initid, args, is_null, is_error ); } @@ -126,13 +110,24 @@ void udf_count_clear( UDF_INIT* initid, char* is_null, char* is_error ) { - data *buffer = (data*)initid->ptr; + data *buffer; + if (initid->ptr == NULL) { + buffer = new data; + if (buffer == NULL) { + *is_error = 'm'; + return; + } + initid->ptr = (char*)buffer; + } else{ + buffer = (data*)initid->ptr; + } + buffer->count = 0; *is_null = 0; *is_error = 0; - buffer->logfile << "udf_count_clear() initid " << initid << " internal id " << buffer->id << " done" << endl; + logfile << "udf_count_clear() initid " << initid << " internal id " << buffer->id << " done" << endl; } @@ -144,7 +139,7 @@ { long long x = *((long long *)args->args[0]); buffer->count++; - buffer->logfile << "udf_count_add() initid " << initid << " internal id " << buffer->id << " added x " << x << " attribute " << args->attributes[0] << " count " << buffer->count << endl; + logfile << "udf_count_add() initid " << initid << " internal id " << buffer->id << " added x " << x << " attribute " << args->attributes[0] << " count " << buffer->count << endl; } } @@ -153,6 +148,7 @@ long long udf_count( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* is_error ) { data* buffer = (data*)initid->ptr; + long long count; if (buffer == NULL) { *is_null = 1; @@ -162,20 +158,25 @@ if (buffer->count == 0) { *is_null = 1; - buffer->logfile << "udf_count() " << buffer->id << " count was 0" << endl; + logfile << "udf_count() " << buffer->id << " count was 0" << endl; return 0; } if (is_error && *is_error != 0) { *is_null = 1; - buffer->logfile << "udf_count() " << buffer->id << " is_error was not 0" << endl; - return 0.0; + logfile << "udf_count() " << buffer->id << " is_error was not 0" << endl; + return 0; } - buffer->logfile << "udf_count() initid " << initid << " internal id " << buffer->id << " count " << buffer->count << endl; + logfile << "udf_count() initid " << initid << " internal id " << buffer->id << " count " << buffer->count << endl; *is_null=0; + count=buffer->count; + + // free(buffer); + delete buffer; + initid->ptr = NULL; - return buffer->count; + return count; }