Welcome to %s forums

BrainModular Users Forum

Login Register

Ram increase pb probably due to "new" operator

Create your own modules in C++
Post Reply
23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 09 Jun 2015, 01:44

im facing a ram increase pb i fail to solve,

it's in a synth code ,in some part od the code it looks like each time a new buffer is created it adds to memory with no release, until reaching limits and crash.
i tried to use 'delete' operator both with [] or not , but it doesn't semm to do something :( .a bit stucked from a while.. any ideas?

the code is very wide but it seems this isolated part seemms to be the problem:
(to summup it's an up-scaling array function to process on lows sizes array in order trying to save some cpu then re-stretch.

Code: Select all

//////
        .....
	double* S1_HI  = new double[SMP_DUR_LOW * reduxa];
        .....
        S1_HI = UPSCALE_16X&#40;S1_LOW, SMP_DUR_LOW&#41;;          //   <---------- memory increase stops when bypassing
       ......
      	delete&#91;&#93; S1_HI;
	S1_HI = nullptr;
    
       // this calls those functions&#58;
//////////////////////////////////////////////////////
double* BEZIER_MULTI&#58;&#58;UPSCALE_2X&#40;double *Input,int sizein&#41;
&#123;

	int sizeOut = sizein * 2;
	double* TMP_OUT = new double&#91;sizeOut&#93;;
	double valin;
	int j;
	for &#40;int i = 0; i < sizein; i++&#41;
	&#123;
		j = i * 2;
		valin = Input&#91;i&#93;;
		TMP_OUT&#91;j&#93; = valin;
		if &#40;i < &#40;sizein-1 &#41;&#41;
		&#123;
			TMP_OUT&#91;j + 1&#93; = &#40;valin + Input&#91;i+1&#93;&#41; / 2;
		&#125;
		else
		&#123;
			TMP_OUT&#91;j + 1&#93; = valin +&#40;&#40;valin - Input&#91;i - 1&#93;&#41; / 2&#41;;
		&#125;
	&#125;
	return TMP_OUT;
	delete TMP_OUT;
	TMP_OUT = nullptr;
&#125;
//////////////////////////////////////////////////////////////
double* BEZIER_MULTI&#58;&#58;UPSCALE_4X&#40;double *Input, int sizein&#41;
&#123;
	int sizeOut = sizein * 4;
	double* TMPb_OUT = new double&#91;sizeOut&#93;;
	TMPb_OUT = UPSCALE_2X&#40;UPSCALE_2X&#40;Input, sizein&#41;,sizein*2&#41;;
	return TMPb_OUT;
	delete TMPb_OUT;
	TMPb_OUT = nullptr;
&#125;
/////////////////////////////////////////////////////////////////
double* BEZIER_MULTI&#58;&#58;UPSCALE_8X&#40;double *Input, int sizein&#41;
&#123;
	int sizeOut = sizein * 8;
	double* TMPc_OUT = new double&#91;sizeOut&#93;;
	TMPc_OUT = UPSCALE_4X&#40;UPSCALE_2X&#40;Input, sizein&#41;, sizein * 2&#41;;
	return TMPc_OUT;
	delete TMPc_OUT;
	TMPc_OUT = nullptr;
&#125;
//////////////////////////////////////////////////////////////////
double* BEZIER_MULTI&#58;&#58;UPSCALE_16X&#40;double *Input, int sizein&#41;
&#123;
	int sizeOut = sizein * 16;
	double* TMPd_OUT = new double&#91;sizeOut&#93;;
	TMPd_OUT = UPSCALE_4X&#40;UPSCALE_4X&#40;Input, sizein&#41;, sizein * 4&#41;;
	return TMPd_OUT;
	delete TMPd_OUT;
	TMPd_OUT = nullptr;
&#125;
////////////////////////////////////////////////////////

shawnb
Member
Posts: 190
Location: San Francisco
Contact:

Unread post by shawnb » 09 Jun 2015, 03:10

I'm guessing here... I've seen similar issues before.

Two options, I think...

1) Dont use new:
http://stackoverflow.com/questions/8839 ... mory-leaks

2) Find a way to significantly reduce the # of times it's called, such as a one time allocation.
Address the process rather than the outcome. Then, the outcome becomes more likely. - Fripp

caco
Member
Posts: 306
Contact:

Unread post by caco » 09 Jun 2015, 09:34

Your problem is that you are dynamically allocating memory using the new construct yet you are not deleting it correctly afterwards. Hint, once a return statement is reached no other code in the function will get processed so you are not triggering the delete statements to free up the ram.

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 09 Jun 2015, 09:57

ah ok thanks a lot for the hints guys!! arf i was doing lo's of 'new' stuff in functions for dynamic sizes purposes.. serems i got to proceed diferently

caco
Member
Posts: 306
Contact:

Unread post by caco » 09 Jun 2015, 16:44

Maybe take a look at autopointers, they could make things a little easier for you when you restructure the code

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 09 Jun 2015, 19:07

thanks caco don't know this feature yet gonna look.

for now i could get it working via having a fixed sized single array defined at start with headroom i modify in functions, but dynamic sizes create/delete would be nice,
this seems to be the feature for not too much pain will test.. find it a bit strange/hard in c++ to pass arrays from/to functions, the pointer stuff, ram ect arf..still learning

caco
Member
Posts: 306
Contact:

Unread post by caco » 09 Jun 2015, 23:25

Everyone finds pointers odd to start with, and suddenly it all clicks and you wonder how you ever managed without them :)

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 10 Jun 2015, 01:22

ah ok ,good to know, slowly catch the advantages, just have to get rid of old habits then^^

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests