Solved

Strange weird memory problem or something

Posted on 2000-05-04
3
331 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
ID: 2779830
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
ID: 2779858
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
ID: 2780119
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using Delphi code in a Builder project that uses a DLL 3 110
SetCurrentDirectory path limit 7 105
returning a dereferenced pts in C++ 10 142
c++, dynamic object by json 1 23
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

15 Experts available now in Live!

Get 1:1 Help Now