Skip to main content
deleted 10 characters in body
Source Link
Adam Stelmaszczyk
  • 19.9k
  • 4
  • 72
  • 111

In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;)

We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.

I know this is not a great way and there are a few catches. (For instance, if you are freeing memory which was allocated by a platform API call, your allocation count will not exactly match your freeing count. Of course, then we incremented the counter when calling API calls that allocated memory.)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Cheers

In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;)

We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.

I know this is not a great way and there are a few catches. (For instance, if you are freeing memory which was allocated by a platform API call, your allocation count will not exactly match your freeing count. Of course, then we incremented the counter when calling API calls that allocated memory.)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Cheers

In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;)

We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.

I know this is not a great way and there are a few catches. (For instance, if you are freeing memory which was allocated by a platform API call, your allocation count will not exactly match your freeing count. Of course, then we incremented the counter when calling API calls that allocated memory.)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Post Closed as "not constructive" by casperOne
Grammar, punctuation and clarity.
Source Link
Tommy Herbert
  • 21.1k
  • 14
  • 56
  • 59

In unmanaged C/C++ code, what are the best practices to detect memory leaks? andAnd coding guidelines to avoid? (asAs if itsit's that simple ;)

weWe have used a bit of a silly way in the past. Having: having a counter incrementsincrement for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.

I know this is not a great way and there are a few catches are there. (For instance, if you are freeing memory which was allocated by thea platform apiAPI call, your allocation count iswill not exactly reflecting the correct number of matching freematch your freeing count. OfcourseOf course, then we incremented the counter when calling such API calls which allocatesthat allocated memory.)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Cheers

In unmanaged C/C++ code, what are the best practices to detect memory leaks? and coding guidelines to avoid (as if its that simple ;)

we have used bit silly way in past. Having a counter increments for every memory allocation call and decrement while freeing. At the end of the program, counter value should be zero.

I know this is not a great way and few catches are there. (For instance, if you are freeing memory which was allocated by the platform api call, your count is not exactly reflecting the correct number of matching free. Ofcourse, then we incremented the counter when calling such API calls which allocates memory)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Cheers

In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;)

We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero.

I know this is not a great way and there are a few catches. (For instance, if you are freeing memory which was allocated by a platform API call, your allocation count will not exactly match your freeing count. Of course, then we incremented the counter when calling API calls that allocated memory.)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Cheers

Post Made Community Wiki by CommunityBot
Source Link
prakash
  • 59.8k
  • 26
  • 95
  • 115

How do you detect/avoid Memory leaks in your (Unmanaged) code?

In unmanaged C/C++ code, what are the best practices to detect memory leaks? and coding guidelines to avoid (as if its that simple ;)

we have used bit silly way in past. Having a counter increments for every memory allocation call and decrement while freeing. At the end of the program, counter value should be zero.

I know this is not a great way and few catches are there. (For instance, if you are freeing memory which was allocated by the platform api call, your count is not exactly reflecting the correct number of matching free. Ofcourse, then we incremented the counter when calling such API calls which allocates memory)

I am expecting your experiences, suggestions and maybe some references to tools which simplify this.

Cheers