Break after certain memory limit hit

Break after certain memory limit hit

Postby FreakNigh » 25 Aug 2011, 12:51

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.
FreakNigh
 
Posts: 79
Joined: 23 Jun 2011, 08:45
Location: Philadelphia, USA

Re: Break after certain memory limit hit

Postby charlie » 25 Aug 2011, 13:01

Asking on www.stackoverflow.com is your best bet.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: Break after certain memory limit hit

Postby MyEmail » 01 Sep 2011, 18:53

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);
}
MyEmail
 
Posts: 107
Joined: 06 Jul 2011, 08:58

Re: Break after certain memory limit hit

Postby FreakNigh » 01 Sep 2011, 19:16

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.
FreakNigh
 
Posts: 79
Joined: 23 Jun 2011, 08:45
Location: Philadelphia, USA

Re: Break after certain memory limit hit

Postby MyEmail » 01 Sep 2011, 19:52

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.
MyEmail
 
Posts: 107
Joined: 06 Jul 2011, 08:58

Re: Break after certain memory limit hit

Postby qubodup » 01 Sep 2011, 23:02

[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.
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Who is online

Users browsing this forum: No registered users and 1 guest