CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2007
    Location
    Baroda,India
    Posts
    74

    Question List iteration Problem

    Hi All,

    I am using list class in my project(MFC Application). I have made list like .....

    typedef std::list <RECT> rectlist; // Here list is of type RECT

    in one header file. And during my project execution I am storing rectangles in the list.

    And now,When I use list iterator in another class like below....( I have already included its header file in using class)

    rectlist::iterator i;

    And i use this iterator like below in any other function......

    for (i = rects.begin(); i != rects.end(); i++)
    {
    CaptureScreen(*i, m_mainbuff); // Error is here
    }

    I got error msg like ...

    list iterator not dereferencable

    at runtime in debug assertion Failed Box.

    Thanks in Advance...

    Ashish

  2. #2
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: List iteration Problem

    Paste more code to show where/how rects is coming from.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  3. #3
    Join Date
    Oct 2007
    Location
    Baroda,India
    Posts
    74

    Re: List iteration Problem

    I have a function like below...

    CheckRects(RTMPRegion &rgn,rectlist &rects)
    {
    rectlist::iterator i;

    for (i = rects.begin(); i != rects.end(); i++)
    {
    CaptureScreen(*i, m_mainbuff);
    }
    }

    calling function .........

    CheckRects(rgn, rectsToScan);


    Now.....
    Prototype of CaptureScreen()...

    void CaptureScreen(RECT &UpdateArea, BYTE *scrBuff);

    And also here when I convert *i like below....

    CaptureScreen((RECT &)i, m_mainbuff);

    then error does not come but when for loop goes to increment the value of i
    then it gives the error like

    list iterator is not incremental

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

    Re: List iteration Problem

    Quote Originally Posted by ashishbhatt12
    And also here when I convert *i like below....

    CaptureScreen((RECT &)i, m_mainbuff);
    You don't need to convert anything.

    When you pass a reference, you pass the item. Since *i is a RECT, you pass *i.
    Code:
    CaptureScreen(*i, m_mainbuff);
    Note that this has nothing to do with RECTS. You just need to know what it means when a function takes a reference to an object as a parameter.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: List iteration Problem

    I do not see anything wrong with the code that you have pasted so far. You will have to send in more code to show us where that rectangle list is coming from - how it is created, how it was populated, how it came into being. I suspect that there is something wrong with that list.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  6. #6
    Join Date
    Oct 2007
    Location
    Baroda,India
    Posts
    74

    Re: List iteration Problem

    **************
    Sorry,but i don't realy know how to use code tags in this forum


    // Get list of rectangles for checking


    rectlist rectsToScan;
    m_region.Rectangles(rectsToScan);

    I use this two lines to get recangles. Here m_region is object of RTMPRegion class in which I am creating recangles.

    And here is Rectangles() function......



    BOOL RTMPRegion::Rectangles(rectlist &rects)
    {
    int buffsize;
    DWORD x;
    RGNDATA *buff;

    // If the region is empty then return empty rectangle list
    if (region == NULL)
    return FALSE;

    // Get the size of buffer required
    buffsize = GetRegionData(region, NULL, 0);
    buff = (RGNDATA *) new BYTE [buffsize];
    if (buff == NULL)
    return FALSE;

    if (GetRegionData(region, buffsize, buff))
    {
    for (x=0; x<(buff->rdh.nCount); x++)
    {
    // Obtain the rectangles from the list
    RECT *rect = (RECT *) (((BYTE *) buff) + sizeof(RGNDATAHEADER) + x * sizeof(RECT));



    rects.push_front(*rect);
    }
    }


    // Delete the temporary buffer
    delete [] buff;

    return !rects.empty();
    //return TRUE;
    }



    In this class I create rectangles from region and store it into the lilst.

    Thanks for taking interest.

    Ashish
    Last edited by ashishbhatt12; October 16th, 2007 at 03:58 AM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: List iteration Problem

    Please edit your post to use code tags. The code you posted is unreadable.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: List iteration Problem

    Is there any multithreading involved, by any chance?
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  9. #9
    Join Date
    Oct 2007
    Location
    Baroda,India
    Posts
    74

    Re: List iteration Problem

    There is no any multithreading used in my application in my project.

    But, I have used windows Hooks used as dll which runs as other project in workspace and yes on which my main MFC project is dependant.

    Thanks.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: List iteration Problem

    Quote Originally Posted by ashishbhatt12
    **************
    Sorry,but i don't realy know how to use code tags in this forum
    When you edit your post, do you see the "Code" icon above the post window?

    The code tags use square brackets, not angle brackets.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: List iteration Problem

    Quote Originally Posted by ashishbhatt12
    **************
    // Get list of rectangles for checking
    My feeling is that you're doing a lot more in-between when you call this function, and when you're iterating through the list, causing the problem.
    Code:
    #include <list>
    #include <vector>
    #include <windows.h>
    
    typedef std::list<RECT> rectlist;
    
    HRGN region;
    
    BOOL Rectangles(rectlist &rects)
    {
    	int buffsize;
    	DWORD x;   
    
    	// If the region is empty then return empty rectangle list
    	if (region == NULL)
    		return FALSE;
    	
    	// Get the size of buffer required
    	buffsize = GetRegionData(region, NULL, 0);
        std::vector<RGNDATA> buff( buffsize );
    
    	if (GetRegionData(region, buffsize, &buff[0]))
    	{
    		for (x=0; x<(buff[0].rdh.nCount); x++)
    		{
    			// Obtain the rectangles from the list
    			RECT *rect = (RECT *) (((BYTE *) &buff[0]) + sizeof(RGNDATAHEADER) + x * sizeof(RECT));
    			rects.push_front(*rect);
    		}
    	}
    	return !rects.empty();
    }
    
    void SomeFunction(RECT& theRect)
    {
        //
    }
     
    int main()
    {
        rectlist rectsToScan;
        Rectangles(rectsToScan);
        rectlist::iterator i = rectsToScan.begin();
        for (i = rectsToScan.begin(); i != rectsToScan.end(); ++i)
            SomeFunction(*i); 
    }
    Here is a compilable, but simple version of what you posted (I used vector instead of new[] and delete[], and I didn't set "region" to anything). Given the above, add whatever needs to be added to get it to do the work.

    Now that the code is isolated away from anything else you haven't posted, does the code above work? If it does, then the problem is not iteration -- it is something else in your code that you didn't post that you're doing.

    Regards,

    Paul McKenzie

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured