Detecting memory leaks

Hi you all,

I have a trouble which is stalling me for a long time.

I am constructing a DLL in C++, which is called from a Mathematica package. The package returns an error, meaning that my program throw an exception. The package support staff told me that I must have memory leaks in my code. The problem is that I have remodelled my code completely. I mean, when I took it, it was a very large 6000 lines cpp file. Now it is OO and it have good practise in software engineering. Of course, it is a legacy and I did something wrong translating the code from the old version.

I have some questions about it:

1) When I make a new [] inside a function, and then I return the pointer which I reserve the memory; Is it correct to make a delete [] outside the function, in order to free the previously reserved memory? I hope so...

2) Suppose I have no memory leaks in the whole code. Could the library throw an exception because there is no free memory when I make a new? I know that is a strange question, but really I don't see memory leaks in my code and I don't know what could be the problem...

3) There is a couple of function which are used in the library. The Mathematica package call to those function like 100000 times consecutively. If I instance a class each time, I guess that object will be destruct when the function be left in each iteration (considering I declare a destructor of the class, without implementation of it). Is not it?

Thanks a lot, but I am having some understanding problems about destructors, deletes and memory leaks...

Pablo
1) You can but this is generally bad practice. It usually best to delete a pointer in the same scope it is defined. I would advise passing the pointer in your function or a pointer pointer if you need to allocate / modify the pointer itself in your function. This is a reasonable suspect for memory leaks

2)That is possible, i've rarely seen this happen but i guess it can't be ruled out

3) So long as it's an automatic/local object (i.e you didn't create it using new) then it will be deleted when you leave its local scope, otherwise you have to delete it yourself. One thing to look out for as well is that if the function from this library allocates new memory. In this case there should be a function for freeing whatever memory the function allocated. Functions that are the usual suspects for this take in a pointer pointer as a parameter e.g func(type **var)
Definetely I don't think that my trouble be in memory leaks. I observe the memory growing and it is no so big! Of course, I may have someone, but it is no critical!

What worries to me now is that I don't manage capture the exception which provokes the crash between the library and Mathematica.

Now, I am putting try-catch(...) sentences everywhere where I am doing news! Which represents almost the complete code!

I used the Dr. Watson toll (in Windows), but when program crashes it doesn't help me too much! I generates a dump file which I can open in VS2005, but I can not reproduce the crash again with that dump file.

Now I am trying intensively the dll with those try-catch(...) exceptions!

Any advise will be welcome!
My try-catch(...) sentences don't work! :(

I continue looking for memory leaks with the VS2005 debugging tool.

I put the following in the beginning of my source:
1
2
3
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h> 


And then:
1
2
3
4
5
6
int main(int argc, _TCHAR* argv[])
{

	//LEAKS
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );


It detects a lot of small memory leaks but, it doesn't put the file name and the line number. I tried to put a breakpoint in a memory allocation, since the output windows tell me the number of block, but it doesn't work neither.

I have tried with Vld, but it continues without telling me the file and the line number!

I don't know why the dll crashes and I don't know what else try in order to find the fail!

Thanks a lot,

Pablo

Hi,

I am a bit closer to the solution of my trouble.

Basically, I analyse the crash with Dr. Watson tool, and what it tells me is that trouble is in:

UnRiskAdd3!std___Allocate<char>

It means that it fails in Allocate<char>. For me it is completely incomprehensible since I have no memory leaks, and the program is not going slower or consuming a lot of memory!

Why could this standard Allocate function fails?

Any clue will be welcome!

Thanks a lot!

Topic archived. No new replies allowed.