Memory leak using strings

Hi, I need to write a script using c++ to interact with a software package. I am not that experienced with c++ though and run into a problem.

When I run the script my RAM usage keeps increasing linear in time until all RAM is being used and the script crashes. I thus suspect a memory leak.

In the script I have several functions, one of them is posted below. Other look similar. I suspect the problem to be the string ArrayDiabolo. I use it in several loops. Sometimes I let it contain 2 strings, sometimes 4. Is it possible that every time I set the values for ArrayDiabolo the old ones are not deleted form the RAM?

And if this is not the problem does anyone see another problem that can cause the memory leakage?

Many thanks for helping!

Job


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
void importGeometries()
{
	//set al diabolos to the right place (they all start at position (0,0,0))
	string selectString;
	for( int i = 0; i <= (amountDiabolos-1); i++)
		{
				for (int a = 0; a < 2; a++)	//loop for diabolos and rings
				{
					
					string ArrayDiabolo[2] = { ("diaboloL("+ to_string((_ULonglong)i)+")"), ("diaboloR("+ to_string((_ULonglong)i)+")")};
					selectString = ArrayDiabolo[a];
					coupling.getGeometryId(selectString.c_str(),selected);
					C3dValue move = C3dValue(	cos(diaboloAngle)*(i-1)*diaboloDist,		//movement in x direction
												0,											//movement in y direction
												sin(diaboloAngle)*(i-1)*diaboloDist);		//movement in z direction
					coupling.setGeometryMotion(selected,move,zeroMatrix,zeroVector,zeroVector,0);
					

				
					if ( i % 2== 0 )	//loop for Rings (only on even positions)
					{
						string ArrayRing[2] = { ("ringL("+ to_string((_ULonglong)i/2)+")"), ("ringR("+ to_string((_ULonglong)i/2)+")")}; //i/2 to call voor Ring(0)Ring(1).. instea (0)(2) etc
						selectString = ArrayRing[a];
						coupling.getGeometryId(selectString.c_str(),selected);
						C3dValue move = C3dValue(	cos(diaboloAngle)*(i-1)*diaboloDist,		//movement in x direction
													0,											//movement in y direction
													sin(diaboloAngle)*(i-1)*diaboloDist);		//movement in z direction
						coupling.setGeometryMotion(selected,move,zeroMatrix,zeroVector,zeroVector,0);
					}
				
					
				}

		}
}
Last edited on
You will cause a memory leak if you allocate memory using new or malloc, and you never tidy it up afterwards with delete or free.

Unless there is a leak inside one of those functions you're calling, there are no leaks here.

If you build with debug symbols, and run your program under valgrind, it will tell you about memory leaks.
Last edited on
Topic archived. No new replies allowed.