Sign in to follow this  

Problem with Pointers in my App

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Quote:
Original post by joseka17
My 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 this post


Link to post
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 this post


Link to post
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

Thanks for reply



Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Thank for the help and well I have 2 other question .1)Do i have to initialize my pointer if i use an stl vector instead of reallocating it (Well i guest i have to initializate anyway)?? and another question is .. what happen if i register a window class variable with lets say... the name "win1" and then i fill the same windows class variable with the name "win2" and register it?? Could it be possible? Could it give me problems unregistering it??(i save the registered names in a stl vector).

Regards Joseka

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this