VS2005 MFC Debugger Stopping Time
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: VS2005 MFC Debugger Stopping Time

  1. #1
    Join Date
    Jan 2011
    Location
    Durham, UK
    Posts
    23

    VS2005 MFC Debugger Stopping Time

    Hi all,

    I've been writing an MFC programme in VS2005 in C++ and have recently run into some issues during debugging. When I debug the code it will run perfectly well until the point I end the debugging process (either by closing the MFC window or through the menus/toolbars within Visual Studio). Visual Studio will take quite some time to finish the debugging process and give me full control of the programme again. The sort of time I'm meaning can vary from 5-10 seconds to many, many minutes (so long that last night I had to leave my computer on overnight and came back this morning to find that it had finished - in other words, too long to sit around and wait but it's not completely hanging, it will recover eventually).

    Basically, since I'm not a vastly experienced programmer, I'd like some information as to why this might be. Is it a problem with VS2005? Is it due to my code? Or is it something else?

    I've seen various posts on the web about problems with the VS2005 debugger, but can't find one about this specific problem (e.g. lots about problems stepping between breakpoints, but my actual code runs fine up to the point when I want to stop debugging).

    I'm not expecting anyone to look through the masses of code to find an error, as I doubt it is any specific lines since the programme compiles and executes fine, but I'm wondering two things:

    1. What is it that the debugger actually does after I click "stop debugging"
    2. Could the problem be due to memory allocation (as I'm estimating at the moment)

    For question 1, I'd quite like to find out what it's doing. For question 2, I can tell you that my code creates several 1D and 2D arrays during it's operation, but I delete them asap (which I though would mean the debugger would have less work to do itself at the end to delete everything). The methods I use to create and delete the arrays are as follows:

    // create 1D array
    double *new1DArray;
    new1DArray = new double[nRows];

    // create 2D array
    double **new2DArray;
    new2DArray = new double*[nRows];
    for(row = 0; row < nRows; row++) new2DArray[row] = new double[nColumns];

    // delete 1D array
    delete[] new1DArray;

    // delete 2D array
    for(row = 0; row < nRows; row++) delete[] new2DArray[row];
    delete[] new2DArray;

    The reason why I think this could be the cause is that the time taken to "stop debugging" (as I said above, it varies greatly) increases dramatically once I increase the scale of the problem that my programming is tackling - in other words, as the scale of the problem increases, the sizes of the arrays increase and also the number of arrays created and deleted increase too.

    I hope this all makes sense, and that you can see what my issue is. Any assistance in finding a solution, or advice in how to better program to avoid this problem, will be greatly appreciated.

    Many thanks,

    Jonathan

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: VS2005 MFC Debugger Stopping Time

    Usually it's dumping out memory leak information. You can interrupt it by clicking the Stop Debugging button on the debugger toolbar.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,423

    Re: VS2005 MFC Debugger Stopping Time

    You are seriously fragmentiing the memory if the arrays get larger. There are much better ways of doing what you're trying to do.

    First, if possible, you should use a container class such as std::vector, so that memory allocation is not done by you, but the container class itself. The 1-dimensional arrays you're creating "by hand" is, in this day and age of C++, not necessary, as this:
    Code:
    typedef std::vector<double> Array1D;
    ..
    
    Array1D new1DArray( nRows );
    is the equivalent of what you were doing to create the 1-d arrays.

    In addition, here is a much better way of creating those 2-d arrays:
    Code:
    // create 2D array
    double **new2DArray;
    new2DArray = new double*[nRows];
    double *aData = new [nRows * nColumns];
    for(row = 0; row < nRows; row++, aData += nColumns) 
         new2DArray[row] = aData;
    //...
    // delete 2D array
    delete [] new2DArray[0];
    delete [] new2DArray;
    Do you see the difference? Regardless of the number of rows and columns, only 2 calls to new[] are done, and only 2 calls to delete[] are done. The reason is obvious -- the data for the array is created with just one allocation. After that, the row pointers point inside of this data.

    Compare this to your original routine -- if the number of rows was 1,000, you would be calling the allocator 1,001 times, 10,000 it would be calling the allocator 10,001 times, etc.

    If you did this, does the debugger respond much faster?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 1999
    Posts
    27,423

    Re: VS2005 MFC Debugger Stopping Time

    Just for completion, here is your code written using vectors:
    Code:
    #include <vector>
    typedef std::vector<double> Array1D;
    typedef std::vector<Array1D> Array2D;
    
    int main()
    {
        int nRows, nColumns;
       // assume that nRows and nColumns has valid integers
       //...
      // create 1D array
      Array1D new1DArray( nRows );
    
      // create 2D array
      Array2D new2DArray( nRows, Array1D( nColumns) );
    
      // no need for delete[]
    }
    There is no need to call new[] / delete[] as the std::vector class takes care of those issues.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2011
    Location
    Durham, UK
    Posts
    23

    Re: VS2005 MFC Debugger Stopping Time

    Thanks for your replies.

    @GCDEF:
    The problem is that I can press the "stop debugging" button but that's when the problem starts and the lag starts

    @Paul:
    Thanks for your advice, I had a feeling my coding practise could be out of date. I've updated the program with the vector notation provided, but am unsure how to refer to a specific index within the 2D matrix the vector is now representing.

    I know this is basic stuff, but I thought it was in the same way as an array (e.g. new2DArray[row][column]) as given here http://www.cplusplus.com/forum/articles/7459/) but I get an debug error "vector subscript out of range" (my vectors are definitely the right size) - am I missing a trick here?!

    Cheers,

    Jonathan

  6. #6
    Join Date
    Apr 1999
    Posts
    27,423

    Re: VS2005 MFC Debugger Stopping Time

    Quote Originally Posted by jonathan.allen View Post
    I've updated the program with the vector notation provided, but am unsure how to refer to a specific index within the 2D matrix the vector is now representing.
    It is the same as a regular 2D array.
    I know this is basic stuff, but I thought it was in the same way as an array (e.g. new2DArray[row][column]) as given here http://www.cplusplus.com/forum/articles/7459/) but I get an debug error "vector subscript out of range" (my vectors are definitely the right size) - am I missing a trick here?!
    Let's see the code. If the debug runtime states that your subscripts are out of range, they are. This usually means that you're indexing is not correct -- something you could not have detected if you used normal 2-d arrays.

    BTW, as much as I like that site you have a link to, they show the crappy way of creating a 2d-array using pointers. If the 2-d array is square (all rows have the same number of columns), the way I showed above in post #3 is much faster and less demands are placed on the heap.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 1st, 2011 at 12:51 PM.

  7. #7
    Join Date
    Jan 2011
    Location
    Durham, UK
    Posts
    23

    Re: VS2005 MFC Debugger Stopping Time

    Hi Paul,

    I knew I shouldn't have used the word "definitely" in my last post! It meant that I would have obviously missed one of my lines for sizing a vector and yes, one was wrong (although, it was also wrong when written for pointer arrays but executed without issue!?!) - I blame it on "Friday afternoon syndrome"!

    I've implemented your suggestions with the vectors and things seem to have improved dramatically! I think my code was simply creating copies of arrays far too often and using up incredible amounts of memory. So many thanks for your invaluable input!

    Having said that, I am on my home PC right now which has a much better spec than the one in my office, so that could be contributing - but I'll test it first thing on Monday morning and, unless I post again to say it hasn't worked, it can be assumed that using vectors rather than pointer arrays, and ensuring unnecessary copying isn't occurring, has fixed the problem all ends up!

    Fingers crossed.........

    Jonathan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center