Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Strange weird memory problem or something

Posted on 2000-05-04
3
Medium Priority
?
357 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 60 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

721 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