Page 1 of 1

Break after certain memory limit hit

PostPosted: 25 Aug 2011, 12:51
by FreakNigh
Does anyone know of a way to have visual studio break a c++ project if the exe reaches a certain memory limit? There is some odd bug that instantly uses up as much memory as it can so I figured it I have it break when it hits 200mb that'd work for finding the bug.

Re: Break after certain memory limit hit

PostPosted: 25 Aug 2011, 13:01
by charlie
Asking on www.stackoverflow.com is your best bet.

Re: Break after certain memory limit hit

PostPosted: 01 Sep 2011, 18:53
by MyEmail
Use custom new/delete functions and keep track of the memory usage. If a new call exceeds the limit then you can throw an exception, or trigger the debugger, or whatever fits your style.

EDIT: This will do the job:
EDIT2: Forgot the volatile.
{l Code}: {l Select All Code}
volatile u32 memoryUsage = 0;

inline __attribute__((__malloc__)) void * MemAlloc(size_t size) {
   if((memoryUsage + size) > ACCEPTABLE_LIMIT) {
      // your code to trigger the debugger or whatnot goes here
   }

   void *ptr = malloc(size);

   if (ptr == NULL) {
      logPrintf("[ WARNING ]  malloc returned NULL!\n");
   }

   memset(ptr, 0, size);

   // uses a GCC atomic operation so this is threadsafe
   __sync_fetch_and_add(&memoryUsage, size);

   return ptr;
}

inline __attribute__((__malloc__)) void * operator new(size_t size) {
   return MemAlloc(size);
}

inline __attribute__((__malloc__)) void * operator new [] (size_t size) {
   return MemAlloc(size);
}

Re: Break after certain memory limit hit

PostPosted: 01 Sep 2011, 19:16
by FreakNigh
Thanks! I'm not sure that would handle constructors though would it?

I did get this response from stack overflow -

Are you talking about the working set size or heap memory? Heap memory is easy: The debugging VC++ Runtime has _CrtSetAllocHook, which calls a user supplied function on every memory allocation/reallocation/free call.

http://msdn.microsoft.com/en-us/library/820k4tb8.aspx

You could install the hook and then sum the memory allocations. If you hit your threshold value, you can call _debugbreak() to drop into the debugger.


Not sure how exactly to implement that though.

Re: Break after certain memory limit hit

PostPosted: 01 Sep 2011, 19:52
by MyEmail
I wouldn't know either, I have little-to-no experience with VC++ (I am too Linux-bound). But for my example it should work fine with constructors, they still get called under-the-hood inside your new new() function.

A little hack I use to trigger the callstack is "*(int*)NULL = NULL". Yes its a hack, but it will trigger the debugger and show what is calling new() to create the memory leaks. You could also try running it through valgrind if you have a Linux installation handy (valgrind will list the file and line of the memory leaks, and how many bytes where leaked at each location).

EDIT: One other thing, valgring is amazing but performs terribly with large applications, especially multi-threaded ones that are constantly malloc/freeing memory. In this scenario I find it easiest to trip the debugger.

Re: Break after certain memory limit hit

PostPosted: 01 Sep 2011, 23:02
by qubodup
[picked this thread since it's the last one where MyEmail replied]

MyEmail is now permabanned. Not perfect but necessary.

A reminder: don't feed trolls and don't try to resolve anything by being mean back at somebody. Contact the mods/admins.