Results 1 to 11 of 11

Thread: Deallocation

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    Deallocation

    what's wrong with this code?

    CMyClass *pointer = (CMyClass*)SomeThing

    ...do stuff with pointer


    // Done with pointer
    delete[] pointer

    i get a memory leak without deleting it and i get an access
    violation with it

    any ideas?
    Bababooey
    Tatatoothy
    Mamamonkey

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You should use plain delete (without the brackets), as long as the pointer was allocated with new.

    How is the class constructed?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    i'm not creating the pointer with new,

    the *pointer is of type CMyClass

    if you want the exact example....

    http://www.codeproject.com/dialog/csettingsdlg.asp

    when OK is clicked you need to delete one of the pages
    however i'm gettin memory exceptions like i have mentioned.
    Bababooey
    Tatatoothy
    Mamamonkey

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't arbitrarily delete something. If it was automatically allocated (i.e. on the stack) then it'll get sorted out by the compiler.

    Surely MFC provides a way to delete a page from a property sheet? (FYI I didn't look at the source since it won't mean anything to me, considering I've been avoiding MFC for a while now).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    why do i get memory leaks then?
    Bababooey
    Tatatoothy
    Mamamonkey

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Do you have the specific bit of code where its allocated? If it's created with new then it must be deleted at some point, once anything that uses the pointer has finished with it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    Code:
    CPropPageDir *dirPage = (CPropPageDir*) dlg.AddPage(RUNTIME_CLASS(CPropPageDir), "Directories", IDD_PROP_DIR, "Settings");
    
    	int nResponse = dlg.DoModal();
    	if (nResponse == IDOK)
    	{
    	      // Do some stuff
    	}
    
    delete[] dirPage;
    Bababooey
    Tatatoothy
    Mamamonkey

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I'm confused, the MFC docs say:
    MFC Library Reference

    CPropertySheet::AddPageSee Also
    CPropertySheet Overview | Class Members | Hierarchy Chart | CPropertySheet::RemovePage
    Adds the supplied page with the rightmost tab in the property sheet.

    void AddPage(
    CPropertyPage *pPage
    );
    Parameters
    pPage
    Points to the page to be added to the property sheet. Cannot be NULL.
    ...nothing is allocated here and I couldn't find the function you're using
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    sorry, i'm not using the standard property pages

    i'm using the netscape-like preferences dialog from
    http://www.codeproject.com/dialog/csettingsdlg.asp
    Bababooey
    Tatatoothy
    Mamamonkey

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You shouldn't get a memory leak, the property sheet class deletes all property sheet pointers...

    How do you know you have the leak?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    in the debug output window, it'll say memory leak detected
    and the size of the property sheet
    for some reason it's not doing it now ......
    Bababooey
    Tatatoothy
    Mamamonkey

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width