Solved

Strange weird memory problem or something

Posted on 2000-05-04
3
328 Views
Last Modified: 2012-05-11
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
Comment
Question by:mgdPaul
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 20 total points
Comment Utility
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
 

Author Comment

by:mgdPaul
Comment Utility
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
 
LVL 3

Expert Comment

by:LucHoltkamp
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now