• 13
• 18
• 19
• 27
• 10

# Problem with Pointers in my App

This topic is 4807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, im trying to do a Wrapper to manage Windows with the WIN32 APPI. My problem is that i need to allocate and WNdCLASSEX and a LPSTR dinamically ( I use realloc because the New operator dont reallocate memory) , but when i compile my App a popup appears saying Debug Assertion Failed! , the problem is with the header dbgheap.c with the expression _CrtIsValidHeapPointer(pUserData). I suppose that I am sending bad pointers to the malloc func Well anyway here is part of the code: UINT cVentana::Registrar(LPSTR InNameClass,UINT InCstyle) { m_numreg++; MessageBox(NULL,"PartA","Hi",MB_OK); //Here were is the problem m_wclase=(WNDCLASSEX*)realloc(m_wclase,sizeof(WNDCLASSEX)*m_numreg) m_nombreclase=(LPSTR*)realloc(m_nombreclase,sizeof(LPSTR)*m_numreg) MessageBox(NULL,"PartA","Hi",MB_OK); Hope that somebody can help me Joseka }

##### Share on other sites
Quote:
 Original post by joseka17My problem is that i need to allocate and WNdCLASSEX and a LPSTR dinamically ( I use realloc because the New operator dont reallocate memory)

Don't get me wrong but i think its time you looked into the c++ standard library, here is part of it, i recommend you stop your usage of C-style dynamic arrays & strings move to C++ style dynamic arrays (std::vector) & strings (std::strings), i would change your code to something like this:

#include <vector>#include <string>//....namespace foo {  //....  class Ventana {       typedef std::vector<WNDCLASSEX> vec_of_wcls;       vec_of_wcls winds;       //....   public:      //.....     UINT Registrar(const std::string& InNameClassRef, UINT InCstyle) {         WNDCLASSEX temp_wndc;          //....          winds.push_back(temp_wndc);          //....     }     //....  };};

##### Share on other sites
Quote:
 Original post by joseka17 m_wclase=(WNDCLASSEX*)realloc(m_wclase,sizeof(WNDCLASSEX)*m_numreg) m_nombreclase=(LPSTR*)realloc(m_nombreclase,sizeof(LPSTR)*m_numreg)

What are the values of m_wclase and m_nombreclase that are passed to realloc when you get the error? My guess is that they are uninitialized. In that case, you need to set them to 0 before realloc is called the first time (like in the constructor). And don't forget to free them in the destructor.

##### Share on other sites

To JohnBolton
Yep it work , i fixed putting a m_wclass=NULL in the constructor of my class, but well this is a newbie question but , why i have to initializate my variables??? (i mean if i replace all the code to register and create a windows directly in WinMain as in many tutorials out there, I dont have to init my varibales, Why is that so??)

To snk_kid
wow interesting library , im using it now

##### Share on other sites
You have to initialize them because you use realloc. Well you should initialize them anyway, but realloc does depend on the pointer's previous memory. If you don't init the pionter to null realloc assumes the pointer allreadyu points to existing memory, and very bad things ensue.

From MSDN

realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

Cheers
Chris