Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Strange weird memory problem or something

Ohhhhhh man really running out of points like this.... I wish those programmers I have on my ICQ list would come online, then I can keep my points, lol :)

Well I have a strange (well to me anyways) problem.... I'll first copy&paste some source code so I'll make a little more sense :)
=============================

typedef struct tagDD_VIDEOMODE
{
      int iWidth;
      int iHeight;
      int iBpp;
} DD_VIDEOMODE;

typedef struct tagDD_VIDEOCARD
{
      DD_VIDEOMODE *pVidModes;
      GUID         *pGUID;
      char         szDriverName[256];
      char         szDriverDesc[256];
      int          iModeCount;
} DD_VIDEOCARD;

===========================

And in my class CDlgSelect:

===========================
public:
DD_VIDEOCARD *pddVideoCard;
int          iNumCards;
BOOL         bEnumError;

===========================

lol still with me or am I making this post messy? :) Anyways, next bit of code is an enumeration callback that gets called by DDraw's DirectDrawEnumerateEx()...

===========================

BOOL WINAPI gDDEnumCallback(GUID FAR *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName, LPVOID lpContext, HMONITOR hm)
{
      // If an enumeration error has occurred, return
      if (gpDlg->bEnumError)
            return DDENUMRET_CANCEL;
      
      // Check if this is the first item
      if (gpDlg->iNumCards == 0)
      {
            // Allocate memory
            if ((gpDlg->pddVideoCard = (DD_VIDEOCARD *)malloc(sizeof(DD_VIDEOCARD))) == NULL)
            {
                  MessageBox(NULL, "Not enough free memory", "Error", MB_ICONEXCLAMATION);
                  gpDlg->bEnumError = TRUE;
                  return DDENUMRET_CANCEL;
            }
      }
      else
      {
            // Reallocate memory
            if ((gpDlg->pddVideoCard = (DD_VIDEOCARD *)realloc(gpDlg->pddVideoCard, sizeof(DD_VIDEOCARD) * (gpDlg->iNumCards + 1))) == NULL)
            {
                  MessageBox(NULL, "Not enough free memory", "Error", MB_ICONEXCLAMATION);
                  gpDlg->bEnumError = TRUE;
                  return DDENUMRET_CANCEL;
            }
      }
      
      // Allocate memory for video modes
      if ((gpDlg->pddVideoCard[gpDlg->iNumCards].pVidModes = (DD_VIDEOMODE *)malloc(sizeof(DD_VIDEOMODE))) == NULL)
      {
            MessageBox(NULL, "Not enough free memory", "Error", MB_ICONEXCLAMATION);
            gpDlg->bEnumError = TRUE;
            return DDENUMRET_CANCEL;
      }
      
      // Zero newly allocated memory
      ZeroMemory(&gpDlg->pddVideoCard[gpDlg->iNumCards],          sizeof(DD_VIDEOCARD));
      
      ///////////////////////////////////////////////////////////////////////////////////
      // If I ZeroMemory() this, or do anything else with the memory, then my video cards won't show up in the list... why? //
      ///////////////////////////////////////////////////////////////////////////////////
      //ZeroMemory(gpDlg->pddVideoCard[gpDlg->iNumCards].pVidModes, sizeof(DD_VIDEOMODE));
      
      // Store GUID, driver name and driver description
      gpDlg->pddVideoCard->pGUID = lpGUID;
      
      // Copy strings
      strncpy(gpDlg->pddVideoCard[gpDlg->iNumCards].szDriverName, lpDriverName,        255);
      strncpy(gpDlg->pddVideoCard[gpDlg->iNumCards].szDriverDesc, lpDriverDescription, 255);
      
      // Increment count of video cards
      gpDlg->iNumCards++;
      
      // Return OK (so DDraw continues to enumerate)
      return DDENUMRET_OK;
}

========================

Ohhh kay.... well the problem... I hope it's clear.. in the last bit of copy&pasted code there's a comment that pretty much formulates my problem :)
Anyways, I'll write it here too, in an attempt to make my post a little less messy.... lol impossible I know :)
Ok...
I have a pointer to a struct in my class, which in turn has a pointer to another struct. I allocate memory for the struct DD_VIDEOCARD and then I allocate memory for the struct inside the DD_VIDEOCARD struct (DD_VIDEOMODE)... when I do anything at all with this memory I seem to overwrite memory or something... cause data seems to disappear from the "parent" struct.....
Can anyone please help? Complete source code can be sent/copy&pasted if needed... though I think sending via email or the likes is better cause it's gotten kinda long :)

0
mgdPaul
Asked:
mgdPaul
  • 2
1 Solution
 
LucHoltkampCommented:
One thing, this code is plain (ugly) C, perhaps you should consider learning more of C++, then you would use new ans STL containers and avoid these problems....

But anywhay, I think that the problem is (see comments in code):

BOOL WINAPI gDDEnumCallback(GUID FAR *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName, LPVOID lpContext,
                      HMONITOR hm)
                      {
                      // If an enumeration error has occurred, return
                      if (gpDlg->bEnumError)
                      return DDENUMRET_CANCEL;

                      // Check if this is the first item
                      if (gpDlg->iNumCards == 0)
                      {
                      // Allocate memory
                      if ((gpDlg->pddVideoCard = (DD_VIDEOCARD *)malloc(sizeof(DD_VIDEOCARD))) == NULL)
                      {
                      MessageBox(NULL, "Not enough free memory", "Error", MB_ICONEXCLAMATION);
                      gpDlg->bEnumError = TRUE;
                      return DDENUMRET_CANCEL;
                      }
                      }
                      else
                      {
                      // Reallocate memory
                      if ((gpDlg->pddVideoCard = (DD_VIDEOCARD *)realloc(gpDlg->pddVideoCard, sizeof(DD_VIDEOCARD) * (gpDlg->iNumCards +
                      1))) == NULL)
                      {
                      MessageBox(NULL, "Not enough free memory", "Error", MB_ICONEXCLAMATION);
                      gpDlg->bEnumError = TRUE;
                      return DDENUMRET_CANCEL;
                      }
                      }
########################################
You allocate memory here for the pointer pVidModes,
so pVidmodes points to a DD_VIDEOMODE
########################################
                      // Allocate memory for video modes
                      if ((gpDlg->pddVideoCard[gpDlg->iNumCards].pVidModes = (DD_VIDEOMODE *)malloc(sizeof(DD_VIDEOMODE))) == NULL)
                      {
                      MessageBox(NULL, "Not enough free memory", "Error", MB_ICONEXCLAMATION);
                      gpDlg->bEnumError = TRUE;
                      return DDENUMRET_CANCEL;
                      }
########################################
Here you zero out the structure, overwriting your pointer
gpDlg->ppdVideoCard[gpDlg->iNumCards].pVidModes
########################################
                      // Zero newly allocated memory
                      ZeroMemory(&gpDlg->pddVideoCard[gpDlg->iNumCards],          sizeof(DD_VIDEOCARD));

                      ///////////////////////////////////////////////////////////////////////////////////
                      // If I ZeroMemory() this, or do anything else with the memory, then my video cards won't show up in the list... why? //

########################################
pVidModes now contains a NULL pointer, so next ZeroMemory
will certainly fail, and your memory is corrupted                      
########################################
///////////////////////////////////////////////////////////////////////////////////
                      //ZeroMemory(gpDlg->pddVideoCard[gpDlg->iNumCards].pVidModes, sizeof(DD_VIDEOMODE));

                      // Store GUID, driver name and driver description
                      gpDlg->pddVideoCard->pGUID = lpGUID;

                      // Copy strings
                      strncpy(gpDlg->pddVideoCard[gpDlg->iNumCards].szDriverName, lpDriverName,        255);
                      strncpy(gpDlg->pddVideoCard[gpDlg->iNumCards].szDriverDesc, lpDriverDescription, 255);

                      // Increment count of video cards
                      gpDlg->iNumCards++;

                      // Return OK (so DDraw continues to enumerate)
                      return DDENUMRET_OK;
                      }

0
 
mgdPaulAuthor Commented:
Thanks! :) You have no idea how stupid I felt when I saw your answer.... cause if I'd just looked over it another time I could have found it myself too..... dammit :) My brain didn't wanna cooperate it seems :)
Btw about your comment on my code, this is just something I coded in a few minutes, just the first few sketches... I beautify my code once I get the basis to work....
And I never said my code was good, fast nor pretty
0
 
LucHoltkampCommented:
Glad I could help.
And I just wanted to say that using STL containers avoid these kind of bugs... not putting you down or anything...
Good luck with your program!
Luc
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now