Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Marshaling

Marshaling

Scheduled Pinned Locked Moved C#
performancequestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kastro
    wrote on last edited by
    #1

    I have a control that overrides WndProc. I handle WM_NCCALCSIZE and in the case where LParam is true I need to store a pointer to a value in the Result field of the Message structure. I do so as follows:

    msg.Result = Marshal.AllocHGlobal(4)
    Marshal.WriteInt32(msg.Result, WVR_REDRAW)

    I never call Marshal.FreeHGlobal since the Result is used outside the scope of WndProc. Will this code cause a memory leak? If so, is there any way to do this without a memory leak? Thanks

    R 1 Reply Last reply
    0
    • K Kastro

      I have a control that overrides WndProc. I handle WM_NCCALCSIZE and in the case where LParam is true I need to store a pointer to a value in the Result field of the Message structure. I do so as follows:

      msg.Result = Marshal.AllocHGlobal(4)
      Marshal.WriteInt32(msg.Result, WVR_REDRAW)

      I never call Marshal.FreeHGlobal since the Result is used outside the scope of WndProc. Will this code cause a memory leak? If so, is there any way to do this without a memory leak? Thanks

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      msg.Result = WVR_REDRAW;

      will be just fine

      K 1 Reply Last reply
      0
      • R Rama Krishna Vavilala

        msg.Result = WVR_REDRAW;

        will be just fine

        K Offline
        K Offline
        Kastro
        wrote on last edited by
        #3

        That won't compile for me since Result is an IntPtr and WVR_REDRAW is just an Integer...

        R 1 Reply Last reply
        0
        • K Kastro

          That won't compile for me since Result is an IntPtr and WVR_REDRAW is just an Integer...

          R Offline
          R Offline
          Rama Krishna Vavilala
          wrote on last edited by
          #4

          Ok, msg.Result = new IntPtr(WVR_REDRAW); As far as I'm concerned you are a lower form of life than B*** S***** and M***** M******** combined. Colin Davies on some guy called R*** G****

          K 1 Reply Last reply
          0
          • R Rama Krishna Vavilala

            Ok, msg.Result = new IntPtr(WVR_REDRAW); As far as I'm concerned you are a lower form of life than B*** S***** and M***** M******** combined. Colin Davies on some guy called R*** G****

            K Offline
            K Offline
            Kastro
            wrote on last edited by
            #5

            That doesn't work either.. That creates a pointer to memory location WVR_REDRAW, the value at that location is not controlled by me.. I need to set Result to an IntPtr that points to a location containing the value WVR_REDRAW... The only way that I know of to do this is with the AllocHGlobal and WriteInt32 calls...

            R 1 Reply Last reply
            0
            • K Kastro

              That doesn't work either.. That creates a pointer to memory location WVR_REDRAW, the value at that location is not controlled by me.. I need to set Result to an IntPtr that points to a location containing the value WVR_REDRAW... The only way that I know of to do this is with the AllocHGlobal and WriteInt32 calls...

              R Offline
              R Offline
              Rama Krishna Vavilala
              wrote on last edited by
              #6

              Kastro wrote: That doesn't work either.. No my code was right IntPtr is a value type so you will not have any problems. You don't need AllocHGlobal As far as I'm concerned you are a lower form of life than B*** S***** and M***** M******** combined. Colin Davies on some guy called R*** G****

              K 1 Reply Last reply
              0
              • R Rama Krishna Vavilala

                Kastro wrote: That doesn't work either.. No my code was right IntPtr is a value type so you will not have any problems. You don't need AllocHGlobal As far as I'm concerned you are a lower form of life than B*** S***** and M***** M******** combined. Colin Davies on some guy called R*** G****

                K Offline
                K Offline
                Kastro
                wrote on last edited by
                #7

                From the .NET help docs:

                public IntPtr(
                int value
                );

                value - A pointer or handle contained in a 32-bit signed integer."

                IntPtr is a value type, but the value that it stores is an address. My original code works (presumably leaking memory though). When I switch to using new IntPtr(WVR_REDRAW) it doesn't work...

                R K 2 Replies Last reply
                0
                • K Kastro

                  From the .NET help docs:

                  public IntPtr(
                  int value
                  );

                  value - A pointer or handle contained in a 32-bit signed integer."

                  IntPtr is a value type, but the value that it stores is an address. My original code works (presumably leaking memory though). When I switch to using new IntPtr(WVR_REDRAW) it doesn't work...

                  R Offline
                  R Offline
                  Rama Krishna Vavilala
                  wrote on last edited by
                  #8

                  IntPtr is a value type of size same as the default pointer size for the platform. It can be used for numbers or pointers. All it is is that it size varies the number stored in it can be anything. In window messages LParam, and WParam are 32 bit ints or 64 bit ints depending on the platform. In window messages LParam and WParam mean different things depending on the context. Same thing about LResult it can be used to return integers or pointers. In case of WM_NCCALCSIZE windows expect an integer to be returned so if return as new IntPtr(WVR_REDRAW) it is ok.

                  1 Reply Last reply
                  0
                  • K Kastro

                    From the .NET help docs:

                    public IntPtr(
                    int value
                    );

                    value - A pointer or handle contained in a 32-bit signed integer."

                    IntPtr is a value type, but the value that it stores is an address. My original code works (presumably leaking memory though). When I switch to using new IntPtr(WVR_REDRAW) it doesn't work...

                    K Offline
                    K Offline
                    Kastro
                    wrote on last edited by
                    #9

                    Ok, I figured out what's going on... You are right about just using new IntPtr(WVR_REDRAW).. I used Spy++ to see what result was being returned for the WM_NCCALCSIZE message... For my code I was getting WVR_ALIGNBOTTOM | WVR_REDRAW | 0019F008 From what you suggested I get WVR_REDRAW... What you suggested wasn't working (as far as the control went) because of other reasons... Also is the wording of the help file incorrect then? That's what led me to believe what I did.

                    J 1 Reply Last reply
                    0
                    • K Kastro

                      Ok, I figured out what's going on... You are right about just using new IntPtr(WVR_REDRAW).. I used Spy++ to see what result was being returned for the WM_NCCALCSIZE message... For my code I was getting WVR_ALIGNBOTTOM | WVR_REDRAW | 0019F008 From what you suggested I get WVR_REDRAW... What you suggested wasn't working (as far as the control went) because of other reasons... Also is the wording of the help file incorrect then? That's what led me to believe what I did.

                      J Offline
                      J Offline
                      James T Johnson
                      wrote on last edited by
                      #10

                      Kastro wrote: Also is the wording of the help file incorrect then? Not entirely, IntPtr is used to represent an integer value that varies in size depending on the platform. A specific use of such an integer is a pointer. There isn't anything special about a pointer, except that its value refers to a location in memory. James "Java is free - and worth every penny." - Christian Graus

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups