Skip to content

Commit

Permalink
MB-7629, MB-7656 Delete context object
Browse files Browse the repository at this point in the history
On context destruction, delete as well the context
object, and not just its members.

Change-Id: I25f3fb23d25e242fced7aa2d33d773c896509a12
Reviewed-on: http://review.couchbase.org/25307
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Reviewed-by: Fulu Li <fulu@couchbase.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
  • Loading branch information
fdmanana committed Mar 22, 2013
1 parent e534452 commit 474a47b
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions src/views/mapreduce/mapreduce_c.cc
Expand Up @@ -46,6 +46,11 @@ static std::map<uintptr_t, mapreduce_ctx_t *> ctx_registry;
static pthread_mutex_t ctx_registry_mutex = PTHREAD_MUTEX_INITIALIZER;


static mapreduce_error_t start_context(const char *functions[],
int num_functions,
void **context,
char **error_msg);

static void make_function_list(const char *sources[],
int num_sources,
std::list<std::string> &list);
Expand All @@ -63,23 +68,7 @@ mapreduce_error_t mapreduce_start_map_context(const char *map_functions[],
void **context,
char **error_msg)
{
try {
mapreduce_ctx_t *ctx = new mapreduce_ctx_t();
std::list<std::string> functions_list;

make_function_list(map_functions, num_functions, functions_list);
initContext(ctx, functions_list);
register_ctx(ctx);
*context = (void *) ctx;
} catch (MapReduceError &e) {
copy_error_msg(e.getMsg(), error_msg);
return e.getError();
} catch (std::bad_alloc &) {
copy_error_msg(MEM_ALLOC_ERROR_MSG, error_msg);
return MAPREDUCE_ALLOC_ERROR;
}
*error_msg = NULL;
return MAPREDUCE_SUCCESS;
return start_context(map_functions, num_functions, context, error_msg);
}


Expand Down Expand Up @@ -130,24 +119,7 @@ mapreduce_error_t mapreduce_start_reduce_context(const char *reduce_functions[],
void **context,
char **error_msg)
{
try {
mapreduce_ctx_t *ctx = new mapreduce_ctx_t();
std::list<std::string> functions_list;

make_function_list(reduce_functions, num_functions, functions_list);
initContext(ctx, functions_list);
register_ctx(ctx);
*context = (void *) ctx;
} catch (MapReduceError &e) {
copy_error_msg(e.getMsg(), error_msg);
return e.getError();
} catch (std::bad_alloc &) {
copy_error_msg(MEM_ALLOC_ERROR_MSG, error_msg);
return MAPREDUCE_ALLOC_ERROR;
}

*error_msg = NULL;
return MAPREDUCE_SUCCESS;
return start_context(reduce_functions, num_functions, context, error_msg);
}


Expand Down Expand Up @@ -277,6 +249,7 @@ void mapreduce_free_context(void *context)

unregister_ctx(ctx);
destroyContext(ctx);
delete ctx;
}
}

Expand Down Expand Up @@ -352,6 +325,39 @@ void mapreduce_set_timeout(unsigned int seconds)
}


static mapreduce_error_t start_context(const char *functions[],
int num_functions,
void **context,
char **error_msg)
{
mapreduce_ctx_t *ctx = NULL;
mapreduce_error_t ret = MAPREDUCE_SUCCESS;

try {
ctx = new mapreduce_ctx_t();
std::list<std::string> functions_list;

make_function_list(functions, num_functions, functions_list);
initContext(ctx, functions_list);
} catch (MapReduceError &e) {
copy_error_msg(e.getMsg(), error_msg);
ret = e.getError();
} catch (std::bad_alloc &) {
copy_error_msg(MEM_ALLOC_ERROR_MSG, error_msg);
ret = MAPREDUCE_ALLOC_ERROR;
}

if (ret == MAPREDUCE_SUCCESS) {
register_ctx(ctx);
*context = (void *) ctx;
*error_msg = NULL;
} else {
delete ctx;
}
return ret;
}


static void make_function_list(const char *sources[],
int num_sources,
std::list<std::string> &list)
Expand Down

0 comments on commit 474a47b

Please sign in to comment.