Solved

Strange weird memory problem or something

Posted on 2000-05-04
3
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 175
SNMP error No Such Object available on this agent at this OID 3 454
Header of docx file 17 145
Error creating a new C++ project in ,net 20 60
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

710 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