Deallocating dynamically allocated keys in SGI hash map

Hi everybody,
I am using the SGI implementation of hash maps.
I need to use dynamically allocated char* variables as the key type and I am using a simple structure with two dynamically allocated char* fields as the value type.
Now the problem is as follows: when I am done with the hash map I need to deallocate the dynamically allocated variables, since I am using several very large hash maps in loops and every iteration take hundreds of megs of memory because of memory leaks.
I was able to successfully deallocate the fields of the struct (i.e. the value), thus mitigating the problem, but not the key.
Just trying to delete the key crashes the program.
Well... if you are just running C code, it LOOKS like it's working, but I am calling C code from Matlab through a hex file, and Matlab crashes when I try to deallocate the key.
The point is, I am really unsure how to exactly and completely deallocate the hash map and all the dynamically allocated variables.

What I am doing and is working right now is.
1. Manually deleting the fields of the structure
2. Calling clear() to delete all the values of the hash map, which I am unsure what it really does... however it works.

What is not working, i.e. crashes the program when called from Matlab is:
1. Trying to manually delete the key
2. Trying to call the default destructor, which I am also unsure what it really does... and I don't think it would be really useful anyway.

Also note that, differently from Java hash maps, the SGI hash map does not have a method to delete keys, whereas it has one to delete elements (i.e. erase, or clear).

I spent weeks trying to get around this issue, but I couldn't find any way to solve this; I am not a skilled C/C++ programmer and I almost gave up.

Code is attached, both standard source code and the same thing with the small wrapper for Matlab.

I would be extremely grateful to anybody that could help with this.
Thanks in advance.

-------------------------- cpp file -------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <ext/hash_map>

using namespace __gnu_cxx;

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

struct my_struct_type
{
       char *field1;
       char *field2;
};

typedef hash_multimap<const char*, my_struct_type, hash<const char*>, eqstr> my_map_type;

int main() {
	my_map_type my_map;

	printf("Inserting into hashmap\n");
	for (int i=0; i<3; i++) {
		char *key = new char[7];
		my_struct_type my_struct;
		my_struct.field1 = new char[10];
		my_struct.field2 = new char[10];
		char str_num[3];

		strcpy(key, "");
		strcat(key, "key");
		sprintf(str_num, "%d",i);
		strcat(key, str_num);

		strcpy(my_struct.field1, "");
		strcat(my_struct.field1, "field1_");
		strcat(my_struct.field1, str_num);
		strcpy(my_struct.field2, "");
		strcat(my_struct.field2, "field2_");
		strcat(my_struct.field2, str_num);

		printf("%s, %s, %s\n", key, my_struct.field1, my_struct.field2);

		my_map.insert(my_map_type::value_type(key, my_struct));
	}

	printf("\nRetrieving from hash map\n");
	my_map_type::const_iterator p;
	for (p=my_map.begin(); p!=my_map.end(); p++) {
		printf("%s, %s, %s\n", p->first, (p->second).field1, (p->second).field2);
	}

	printf("\nTrying to deallocate variables and delete hash map\n");
	for (p=my_map.begin(); p!=my_map.end(); p++) {
		delete [] (p->second).field1;
		delete [] (p->second).field2;
		//delete [] p->first;
	}
    my_map.clear();
    //my_map.~hash_multimap();

	printf("\nSucceeded!!!\n");

	return 0;
}



------------------------- mex file -----------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "mex.h"
#include <ext/hash_map>

using namespace __gnu_cxx;

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

struct my_struct_type
{
       char *field1;
       char *field2;
};

typedef hash_multimap<const char*, my_struct_type, hash<const char*>, eqstr> my_map_type;

extern void _main();

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
	my_map_type my_map;

	mexPrintf("Inserting into hashmap\n");
	for (int i=0; i<3; i++) {
		char *key = new char[7];
		my_struct_type my_struct;
		my_struct.field1 = new char[10];
		my_struct.field2 = new char[10];
		char str_num[3];

        strcpy(key, "");
		strcat(key, "key");
		sprintf(str_num, "%d",i);
		strcat(key, str_num);

        strcpy(my_struct.field1, "");
		strcat(my_struct.field1, "field1_");
		strcat(my_struct.field1, str_num);
        strcpy(my_struct.field2, "");
		strcat(my_struct.field2, "field2_");
		strcat(my_struct.field2, str_num);

		mexPrintf("%s, %s, %s\n", key, my_struct.field1, my_struct.field2);

		my_map.insert(my_map_type::value_type(key, my_struct));
	}

	mexPrintf("\nRetrieving from hash map\n");
	my_map_type::const_iterator p;
	for (p=my_map.begin(); p!=my_map.end(); p++) {
		mexPrintf("%s, %s, %s\n", p->first, (p->second).field1, (p->second).field2);
	}

	mexPrintf("\nTrying to deallocate variables and delete hash map\n");
	for (p=my_map.begin(); p!=my_map.end(); p++) {
		delete [] (p->second).field1;
		delete [] (p->second).field2;
		//delete [] p->first;
	}
    my_map.clear();
    //my_map.~hash_multimap();

	mexPrintf("\nSucceeded!!!\n");

}
Topic archived. No new replies allowed.