Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 10, 2016, 01:03:32 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: ListView Control Hangs during Sort  (Read 5183 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
ListView Control Hangs during Sort
« on: October 07, 2009, 03:14:26 PM »
Greetings
   I seem to recall seeing the answer for this once before, I just can't remember where... *Sigh*

   Anyhow, I have an app I'm working on (suite of them actually) that display's data which is queried from a MySQL db. It's written (as usual for me) in straight Win32 API C++ (No .Net/MFC/etc.). The issue I'm having is that when the list box control is sorted by the (14th) Cost column the ListView control locks up untill the app is restarted.

   Now in the various columns I do switch between alphabetical & numeric sorting, which works just fine for the first 11 columns, but... 12 & 13 only sort one direction instead of toggling between asc & desc, and 14 just hangs the LV thread completely. I've been using the same ListView Sort code in several different apps that all work just fine, but have 10 colums or less.

   Which brings me to (the point?) my question. Does anybody recall seeing an issue with (and solution for) ListView controls having more than X number of columns hanging during sort? I swear I've seen this before reguarding a bugg in the LV control ... I just can't remember where or what the solution was.

I'm using MSVS 2005 SP1

Thank You

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,431
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #1 on: October 07, 2009, 03:33:10 PM »
how many rows of data?

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,029
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #2 on: October 07, 2009, 03:43:44 PM »
Never heard of such a bug (but not saying it can't exist :)).

How do you manage the items? virtual mode, or letting the control manage strings internally?
- carpe noctem

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #3 on: October 07, 2009, 04:20:34 PM »
how many rows of data?
Doesn't matter be it 3 or 300 (Most I ever tried was 2000) behavior is the same.

Never heard of such a bug (but not saying it can't exist :)).

How do you manage the items? virtual mode, or letting the control manage strings internally?
If I'm understanding the question correctly, the control is managing internally. I'm not doing anything OwnerDrawn/Clever it's just a fairly straight forward ListView_InsertColumn(...) ListView_InsertItem(...) sort of affair.

This is basically the sort code I'm using (from an older 7 column version of the app):
Code: C++ [Select]
  1. #include "stdafx.h"
  2.  
  3. extern HWND g_hListBox;
  4. extern int nSortDir[7];
  5.  
  6. int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) {
  7.         static char szBuf1[100], szBuf2[100];
  8.         int i1, i2;
  9.  
  10.    //=================================== Retrieve the item text so we can compare it.
  11.   ListView_GetItemText(g_hListBox, lParam1, (int)lParamSort, szBuf1, sizeof(szBuf1));
  12.   ListView_GetItemText(g_hListBox, lParam2, (int)lParamSort, szBuf2, sizeof(szBuf2));
  13.   //============================================= Then Return the Comparison Results.
  14.   if(lParamSort >= 5) { //================= Sort PageCount Columns 5 & 6 as a Number.
  15.      i1 = strtol(szBuf1, 0, 10);
  16.          i2 = strtol(szBuf2, 0, 10);
  17.      if(nSortDir[lParamSort]) //========= Toggle Between ASCENDING & DESCENDING ORDER
  18.             return i1-i2; //====================================== Sort Order ASCENDING
  19.          else
  20.             return i1+i2; //===================================== Sort Order DESCENDING
  21.    }else{ //========================= Sort ALL the Rest (Alphabetically) as a String.
  22.          if(nSortDir[lParamSort]) //========= Toggle Between ASCENDING & DESCENDING ORDER
  23.                 return(_stricmp(szBuf1, szBuf2) * -1); //=============== Sort Order ASCENDING
  24.          else
  25.                 return(_stricmp(szBuf1, szBuf2)); //=================== Sort Order DESCENDING
  26.   }
  27. }

It was probably 2 years ago when I saw the bugg discussion ... thinking that I'd never need to use that many columns I ignored it and moved on to other more (at the time) relevant issues. Now I'm racking my brain trying to remember where I'd seen it because (several columns later) the bugg is now gnawing my ass off :)

Eóin

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
    • View Profile
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #4 on: October 07, 2009, 04:49:47 PM »
A ListView will hang if the CompareProc returns inconsistent results, eg a < b < c but c < a.

In the code posted there seems to be a logic error in line 20, really seems like it should be return i2-i1;

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #5 on: October 07, 2009, 05:32:34 PM »
A ListView will hang if the CompareProc returns inconsistent results, eg a < b < c but c < a.

In the code posted there seems to be a logic error in line 20, really seems like it should be return i2-i1;
Hm... wouldn't that then have to fail consistently for all (numerically sorted) columns? In the program (with the issue) half the columns are sorted numerically and half are sorted alphabetically. Out of the numerically sorted columns there are IP Addresses, (alpha-numeric) serial numbers, and some actual real live plain old numbers ranging from (roughly) -185,456 - 1,456,789 all of which sort just fine ... unless they are in column 14 (13 if you count from 0 - the control does). It just doesn't track that a logic bugg would be that forgiving.

*Shrug*

I'll put that on my todo list and try it in the morning when I get to the office (I'd certainly love a simple answer), but it just doesn't feel right...Perhaps because I'm mentally stuck (or just mental) on the column count bugg.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,431
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #6 on: October 07, 2009, 05:37:54 PM »
i think Eoin may have nailed it.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #7 on: October 07, 2009, 06:11:09 PM »
i think Eoin may have nailed it.
Sorry No Joy.

I decided to remote into the office & try it (the suspense was killing me...). ...I'll give you this much, the sort is a bit smoother. But it still locks up on column 14.

Just to make sure nothing was being missed, I commented out all of the numeric sort code so everything had to be sorted alphabetically. 1-13 = perfect ... 14 = ListView Control hang/frozen solid. :(

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,431
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #8 on: October 07, 2009, 06:21:45 PM »
nSortDir[7]; <--- only goes up to index 0-6 and yet you are sorting on column 14?

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #9 on: October 07, 2009, 06:37:37 PM »
It's an old code sample, the actual project is nSortDir[14];

Strangely (Since I'd been missing that seemingly key point) Id left the original nSortDir[7] in the project while User Requests caused columns to be added and it never caused a problem until adding the last 14th column. I've still got a 13 column version of the program using nSortDir[7] that works just fine (Damn strange in retrospect). That was the first thing I tried when the bugg was discovered.

Just for clarity (Now that I'm logged into the office), here is the most current version of the broken code :):
Code: C++ [Select]
  1. #include "stdafx.h"
  2.  
  3. extern HWND g_hListBox;
  4.  
  5. int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) {
  6.         static char szBuf1[100], szBuf2[100];
  7.         extern int nSortDir[14];//[7];
  8.         int i1, i2;
  9.  
  10.    //=================================== Retrieve the item text so we can compare it.
  11.   ListView_GetItemText(g_hListBox, lParam1, (int)lParamSort, szBuf1, sizeof(szBuf1));
  12.   ListView_GetItemText(g_hListBox, lParam2, (int)lParamSort, szBuf2, sizeof(szBuf2));
  13.   //============================================= Then Return the Comparison Results.
  14.   if((lParamSort == 5)||(lParamSort >= 7)) { // Sort Only PageCount Columns 5 and 7
  15.      i1 = strtol(szBuf1, 0, 10);  //-+++--> & up as a Number. Columns 4 & below and
  16.          i2 = strtol(szBuf2, 0, 10); //--+++--> 6 are Sorted as Words (alphabetically).
  17.      if(nSortDir[lParamSort]) //========= Toggle Between ASCENDING & DESCENDING ORDER
  18.             return i1-i2; //====================================== Sort Order ASCENDING
  19.          else                     // Thanks Eóin - From DonationCoder.com
  20.             return i2-i1;// <--Good/Bad--> i1+i2; //============= Sort Order DESCENDING
  21.    }else{ //========================= Sort ALL the Rest (Alphabetically) as a String.
  22.          if(nSortDir[lParamSort]) //========= Toggle Between ASCENDING & DESCENDING ORDER
  23.                 return(_stricmp(szBuf1, szBuf2) * -1); //=============== Sort Order ASCENDING
  24.          else
  25.                 return(_stricmp(szBuf1, szBuf2)); //=================== Sort Order DESCENDING
  26.   }
  27. }
  28.  
  29. // Check to see if Alpha or Num Sort Locks up more/less/same
  30. /* Note: This resolved a tendence to periodically sort things incorrectly
  31. Eóin Re: ListView Control Hangs during Sort
  32. « Reply #4 on: 10/7/2009 at 04:49:47 PM »
  33. -------------------------------------------
  34.  
  35. A ListView will hang if the CompareProc returns
  36. inconsistent results, eg a < b < c but c < a.
  37.  
  38. In the code posted there seems to be a logic error
  39. in line 20, really seems like it should be return i2-i1;
  40. */

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #10 on: October 07, 2009, 10:31:23 PM »
Okay it looks like the win goes to mouser for his Captain Obvious tip. it seems that the correct (or at least currently working) version is:
extern int nSortDir[]; with no size specified. Which allows the other locally declared reference (int nSirtDir[14]) to define the actual size.

Well... that and if the size is specified in both places...it apparently helps if they both :)ing match...

Thanks to all

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,431
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #11 on: October 07, 2009, 10:40:16 PM »
real credit goes to Eoin for guessing the core problem, which was that the delay was the result of the callback returning inconsistent comparison results.  after that it was just a matter of figuring out why that was happening.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: ListView Control Hangs during Sort
« Reply #12 on: October 08, 2009, 05:59:10 AM »
real credit goes to Eoin for guessing the core problem, which was that the delay was the result of the callback returning inconsistent comparison results.  after that it was just a matter of figuring out why that was happening.
Actually both i2-i1; and i1+i2; worked after the external array item you spotlighted was fixed. Something about that had always bothered me...just not enough to poke it with a stick. At least not until you tipped the scale by drawing attention to something that was obvious, but was also hiding another not-so-obvious boo-boo on my part. (Hint: the local definition of the array that the external reference was pointing to, was neither 7 nor 14 ...  :-[)

Eóin's tip resolved an entirely different global issue, which was a tendency for the numerically sorted columns to be periodically sorted incorrectly. Which is why there is a comment to that effect in the source code I posted above. That comment will remain there in the official project file to remind me to never do that again.