Ownership Model for pointer data?
-
But from how I understand it: Your plugin, as the “user” of the scintilla object, own any such strings (or complicated data structures for a few messages). When you are sending a string to Scintilla, it just needs to exist during the actual call to the message; whatever happens after that should not affect the message call. When you are receiving a string from Sctntilla, the Scintilla Docs are pretty clear when you have to allocate your own memory for that string, and how much memory they will expect. So the memory needs to be allocated before the message, and cannot be freed until after the message; but what you do with the memory from a received string after the message is complete should again be up to you.
Caveats: I am not a “modern C++ expert”, nor am I a plugin author. However, I have written a Perl module that accesses Notepad++ and its Scintilla instances from outside (ie, using external SendMessages) instead of from the inside like a plugin. And what I had to do from that Perl module matches with the description I did, and I’ve never had a memory issue when I follow those sequences.
-
@xzaton-jw said in Ownership Model for pointer data?:
this is already really weird and sketchy style code
SendMessage is part of the Win32 API and this is a C API. C is the language for “real men” who always know what they do. “Real men” do not need a compiler that behaves like a nanny, warning all the time to be careful not to shoot themselves in the foot. And even IF a C programmer shoots himself in the foot, at least nobody else can figure out what he did. See also >> here <<. Irony off.
To answer your question in short: When transmitting a pointer to a bufferr with SendMessage, the ownership of that buffer is NOT handed over to the message receiver.
The long answer is: If it would not be like that, the sending code wouldn’t be able to free the buffer because it can not know at which point in time the receiving code doesn’t need the buffer anymore. The receiving code also wouldn’t be able to free the buffer because it can not know if the buffer’s beginning is at the address the received pointer points to. That pointer could point somewhere into the middle of a larger memory block. Thus, the system would run out of memory soon. If the receiver needs access to the buffer’s content after it has processed the message, it has to make a copy of that content.
-
LET THE ZINGERS COMMENCE!
@xzaton-jw said:
I come from modern C++ so this is already really weird and sketchy style code.
I google-translated this, and found it to say:
I write code but I really don’t know what is going on inside computers. -
@dinkumoil ,
ROFLMAO. I have never read that before and now my sides are hurting. Thanks. :) -
-
@dinkumoil
Thank you for making me feel old, and also for giving me a good laugh. Every time I come across a copy of that list, I can’t resist trying to read it - and giggling so much that I have to stop reading (to resume breathing) before I get halfway thru. 🥲 -
I can’t resist trying to read it
me too :-D
-
classy Gun such shoot much target very bullets is dis giv bullets rly bullets giv length is 0 plz console.error with 'such empty, must reload, wow' amaze wow very bullet is bullets dose pop plz console.loge with "BANG!" plz target.beShot with bullet wow maker bullets dis giv bullets is bullets wow wow classy Bullet maker caliber dis giv caliber is caliber wow wow classy Foot such beShot much bullet plz console.loge with "OUCH!" wow wow very bullet is new Bullet with 45 very gun is new Gun with [bullet] very foot is new Foot gun dose shoot with foot gun dose shoot with foot
-
Writing any compiled language you don’t “know” whats going on inside the computer, you just reason about what it “should” do based on the abstract machine. You don’t know what machine code is generated until you look at the assembler for a given platform :)
-
You don’t know what machine code is generated until you look at the assembler for a given platform
Can’t argue with that…but it isn’t really related to the discussion earlier.
Your original statement:
I come from modern C++ so this is already really weird and sketchy style code.
This is irksome because what @dinkumoil described is really fundamental, and (is one point of many that) should be what everyone learns before handing their trust off to something like “modern C++”. So many people don’t know this, or gloss over it.
I have a good friend that goes on and on about how he loves C++, but he hates C because he just “doesn’t understand that stuff”. I just shake my head…
But, I suppose by virtue of this thread, more of us have answers and the learning process has begun! Cheers!
-
I appreciate the answer, thankyou!
However, I disagree with your premise for why its this way:
You’re postulating that if
::SendMessage
took ownership of the data, it:- Wouldn’t be able to free it because it wouldn’t know when the caller is done with it (UAF)
- Potentially doesn’t point to the start of a block, so you end up leaking memory because it doesn’t clean it all up properly
That’s literally the point of ownership semantics, you can:
- signal it is the callee’s responsibility to clean up when they’re done so no copies need to be made.
- signal its not the callee’s responsibility because you’re doing some weird slicing type shit and they can never clean it up properly.
And in the case that both areas may need to access the data again, you use something like
std::shared_ptr
so its a reference counted shared ownership.So yes, you can do API’s where you transfer ownership of the pointed to data, and they work really well since you’re skipping redundant copies. This isn’t something you can just work out with logical deduction, you either have it or you don’t, and if you don’t, its a bad API.
Its just a shame its written in C so I get fuckin weirdos coming out the woodwork telling me how silly I am because I’m asking questions about stuff they themselves don’t even understand, but I suppose that’s just par for the course when it comes to asking for help in programming forums lol.
-
This post is deleted! -
@xzaton-jw said in Ownership Model for pointer data?:
fuckin weirdos
I suppose that’s me. I’ll wear that banner with pride, I guess.
Or maybe since it was a direct reply to @dinkumoil , maybe him as well…stuff they themselves don’t even understand
…right…
To be fair, though, your reply does seem to indicate that you have more knowledge than my poor friend. So that’s good. :-)
-
@xzaton-jw said in Ownership Model for pointer data?:
fuckin weirdos
Even if you got some harsh criticism, you should not escalate the debate even more by being rude, only to be the bigger badass. Let’s stay civilized.
you can do API’s where you transfer ownership of the pointed to data
I’m pretty sure this can be done, but this was not what your question was about. You asked about the behaviour of the SendMessage function, i.e. the Win32 API. As I told you, this is a C API and C doesn’t provide integrated mechanisms to create an API that works like you prefer it.
you either have it or you don’t, and if you don’t, its a bad API
Just complaining about facts that you can not change and nobody else will change (or do you think MS decides to change the basic principles of the Win32 API after more than 3 decades?) makes no sense. Accept how it is, try to understand its basic principles and learn how to use it.
Its just a shame its written in C
I hope even future operating systems, released by MS or some other company, will have a C based API because C is a high-level programming language that provides only the absolute necessary grade of abstraction from the hardware. Thus, I like to call it a “high-level assembly language”. A C based API allows all other programming languages to use that API. It’s much more difficult to achieve this with a C++ based API because all programming languages would have to be compatible with C++ at the ABI level.
If a higher grade of abstraction from the hardware and the OS API (i.e. Win32) is needed, a framework can provide that. AFAIK, the ATL and MFC frameworks provided by MS don’t do a good job in this regard, but I’m not a C++ dev, so I’m not qualified to make any informed statements about that.
-
@dinkumoil said in Ownership Model for pointer data?:
I hope even future operating systems, released by MS or some other company, will have a C based API because C is a high-level programming language that provides only the absolute necessary grade of abstraction from the hardware. Thus, I like to call it a “high-level assembly language”. A C based API allows all other programming languages to use that API. It’s much more difficult to achieve this with a C++ based API because all programming languages would have to be compatible with C++ at the ABI level.
I’m glad you brought this up. This is exactly what C has been promoted as for years; that it is the next closest language to the metal as you can get short of Assembly Language, without being solely locked to a specific processor. dBASE is an older language and can use the Win32API, precisely for this language allowing us to extend it ourselves if the development vendor ever stopped supporting it. That’s why most FOSS is based on Linux and C. Processor Agnostic
The OP seems to be whining more about having to learn how to do what he wants without having to learn how the machinery works. How Millenial of him. I’ll be waiting with baited breath to see how he rewrites the Win32 API in C++, so he can do what he wants, unhindered by archaic languages. :)
-
@xzaton-jw said in Ownership Model for pointer data?:
Its just a shame its written in C so I get fuckin weirdos coming out the woodwork telling me how silly I am because I’m asking questions about stuff they themselves don’t even understand, but I suppose that’s just par for the course when it comes to asking for help in programming forums lol.
By the way, this isn’t a
programming forum
. This is the Notepad++ Community Forum. Not everyone that uses this forum is a programmer, they are users that use the program for diverse reasons, but then again, you seem to have gotten that wrong as well. Care to try for 3 out of 3? -
This is hard work, all I wanted to know is if I should clear up the data myself, or if the function will do it for me.
If the answer was just:
“It uses the Win32 API which doesn’t take ownership, you need to allocate and deallocate it yourself”
I’d have been happy and on my merry way. Instead I get a thread of garbage.
Anyway:
@dinkumoil said in Ownership Model for pointer data?:
Even if you got some harsh criticism, you should not escalate the debate even more by being rude, only to be the bigger badass. Let’s stay civilized.
Fair point
@dinkumoil said in Ownership Model for pointer data?:
As I told you, this is a C API and C doesn’t provide integrated mechanisms to create an API that works like you prefer it.
You could:
SendMessageTakeOwnership(raw_ptr*)
,SendMessageBorrow(raw_ptr*)
enum OWNERSHIP{BORROWS, TAKES_OWNWERSHP};
SendMessage(OWNERSHIP, raw_ptr*)
And likely a host of other ways of doing it. Ofcourse they aren’t as robust as other ways of doing it, but the point is you can do it in C.
I want to make a another point here, backwards compatibility is an amazing benefit, but it is not free. If there was a way to re-engineer the C API with the knowledge we have about software development today (and it was no effort to do so), I think it would be different to how it is now, and it would include a notion of functions either taking ownership or not.
The simple fact is that its easier/cheaper to document, and not break old code, than to break it to improve it. I think this is the main reason for why it is the way it is, not because they got it perfect first time around.
@dinkumoil said in Ownership Model for pointer data?:
AFAIK, the ATL and MFC frameworks
The fact MS tried to create a framework ontop of the C API demonstrates that it isn’t without its flaws.
@dinkumoil said in Ownership Model for pointer data?:
Just complaining about facts that you can not change and nobody else will change (or do you think MS decides to change the basic principles of the Win32 API after more than 3 decades?) makes no sense. Accept how it is, try to understand its basic principles and learn how to use it.
Fair point
@Lycan-Thrope said in Ownership Model for pointer data?:
The OP seems to be whining more about having to learn how to do what he wants without having to learn how the machinery works. How Millenial of him. I’ll be waiting with baited breath to see how he rewrites the Win32 API in C++, so he can do what he wants, unhindered by archaic languages. :)
I wanted to know how it worked to work around it to achieve what I was trying to do. The post is “what is the ownership model?”, not why “why is the ownership model bad?”, its a subtle difference so no surprise a boomer missed it.
@Lycan-Thrope said in Ownership Model for pointer data?:
By the way, this isn’t a programming forum. This is the Notepad++ Community Forum.
… being pedantic about this is just sad
-
@xzaton-jw said in Ownership Model for pointer data?:
I wanted to know how it worked to work around it to achieve what I was trying to do. The post is “what is the ownership model?”, not why “why is the ownership model bad?”, its a subtle difference so no surprise a boomer missed it.
Considering that the subject of “Ownership Model” is a fairly new aspect specific to “objects”, and C was never written to be Object based or Object Oriented, but is used to create Object-Based and Object-Oriented languages, your lack of knowledge is definitively ‘millenial’ (do it for me, or let me appear superior). C++ was written to extend C into an Object-Oriented language, by it’s very existence as stated by the developer:
Stroustrup is best known for his work on C++. In 1979, he began developing C++ (initially called "C with Classes"). - Bjarne Stroustrup via Wikipedia
Reading further into that same document you’ll find this particularly potent fact about C++ right at the top of the definition of the concepts of C++:
The key language-technical areas of contribution of C++ are:
A static type system with equal support for built-in types and user-defined types (that requires control of the construction, destruction, copying, and movement of objects; and operator overloading). - Wikipedia C++
This, my dear millenial, is called research, and it is what boomer’s learned to do early in life so they didn’t have to be hand held through their learning processes. This is what defines ‘independence’ as opposed to ‘dependence on others’ for answers.
@xzaton-jw said in Ownership Model for pointer data?:
… being pedantic about this is just sad
…as is being asinine.
Fini.
-
There may genuinely be some confusion, let me clarify, when I talk about ownership semantics, I specifically mean:
Who is responsible for “cleaning up” any resource which must be “freed” after it has been “acquired”?
Its part of all languages, whether is a GC, or done manually. It could be dynamic memory, or some I/O like a file or network port. All this stuff about it being related to OOP is nonsense, but I can see why you thought that because RAII is an excellent way to handle it.
As an example, look here: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
Literally an example of passing ownership of a buffer in the win32 API.
And listen man, programming is hard and asking questions is ok because it gives people a chance to learn something new.
Maybe you should save your comments about working everything out on your own for places other than a forum?
And while im here, everytime you’ve used stackoverflow, you’re being a hypocrite, because if it isn’t you asking for help, it’s you benefitting from someone else asking.