Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to create some CPropertyPages to go on a CPropertySheet as follows:

long numberOfPages;
std::vector<myproppage*> pPages;

for( long i=0; i < numberOfPages; i++ )
{
	pPages[i] = new MyPropPage();
	pPages[i]->m_psp.dwFlags |= PSP_USETITLE;
	CString title0(_T("title0"));
	pPages[i]->m_psp.pszTitle = LPCTSTR(title0);
	pPropertySheet->AddPage(pages[l]);

}


In the for loop, the title is initialised correctly. Whne the end of a loop is reached, the title that was just set is lost, and just contains garbage. If I don't have this in a loop the title is not overwritten with garbage. Please could somebody help with this as I am losing the will to live :)
is there a problem with memory that is initialised in a for loop?
Posted
Updated 14-Jun-11 1:28am
v2

If you look at that loop statement, the CString is defined within it so it's probably going out of scope and getting deallocated after the loop. Try either allocating it outside of the loop or allocate the CString on the heap (just be sure to deallocate later).
 
Share this answer
 
Comments
Jackie Lloyd 14-Jun-11 9:35am    
Great - that works. Many thanks. I was trying to find what was going out of scope but didn't think the CString would matter becuase the string is written into the pszTitle variable before the end of the loop. Can you explain why the CString still matters at this stage?
Albert Holguin 14-Jun-11 9:37am    
because you're passing the pointer to it... LPCTSTR <- first two letters, Long Pointer... if you were making a copy, it wouldn't matter
Jackie Lloyd 14-Jun-11 9:45am    
ahhh - I see, thanks again :)
Albert Holguin 14-Jun-11 10:39am    
no prob :)
XML
std::vector<myproppage*> pPages;

and
MIDL
pPages[i] = new MyPropPage();

are not the same thing.

Put
CString title0(_T("title0"));

above and out of your for loop. And you failed to initialize numberOfPages from else where. And its not just CString anymore.
MIDL
pPages[i]->m_psp.pszTitle = title0;
 
Share this answer
 
Comments
Jackie Lloyd 14-Jun-11 8:35am    
Thanks for your comments.
std::vector<myproppage*> should have capitals - sorry, I didn't spot the error in my typing in here. My code does not have this mistake. I have initialised numberOfPages in my code but did not show that - sorry again.

So my code is

<pre>
long numberOfPages = 3;
std::vector<myproppage*> pPages;

for( long i=0; i < numberOfPages; i++ )
{
pPages[i] = new MyPropPage();
pPages[i]->m_psp.dwFlags |= PSP_USETITLE;
CString title0(_T("title0"));
pPages[i]->m_psp.pszTitle = LPCTSTR(title0);
pPropertySheet->AddPage(pages[l]);

}</pre>

I don't understand your comments about the CString - please could you explain. Mnay thanks
Jackie Lloyd 14-Jun-11 8:36am    
why does myproppage have no capitals again - I typed it correctly? Is it because it has <>s round it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900