1. Problem with copying text to the Windows clipboard

I have a strange problem using SetClipboardText() from Greg Haberek's conclip.ew routine upon his suggestion, I am posting it here.

In a Windows console program I use SetClipboardText() to copy short text strings to the clipboard. The first few times this works fine, but after several calls to SetClipboardText the program reproducibly gives wrong results, and finally crashes.

(This only happens with the bound executable, not if I run the program from the Euphoria interpreter.)

Unlikely as this seems, the only explanation I have found for the program's behavior is that data in memory seems to get overwritten by SetClipboardText().

I have no knowledge neither of C nor of the inner workings of Windows nor Euphoria, so I do not know how to pinpoint the problem, let alone do any debugging.

Windows XP, Euphoria 3.1.1, conclip.ew 0.4.

The program where this problem occurs is a command line calculator called Hypatia http://www.drs.at/hypatia

For any help I would be grateful!

Robert Schächter

new topic     » topic index » view message » categorize

2. Re: Problem with copying text to the Windows clipboard

Hi Robert,
I've downloaded your program and tried to reproduce the bug but couldn't do it. Do you have a detailed way to reproduce it?

Cheers,
Guillermo

new topic     » goto parent     » topic index » view message » categorize

3. Re: Problem with copying text to the Windows clipboard

RobertS said...

I have a strange problem using SetClipboardText() from Greg Haberek's conclip.ew routine upon his suggestion, I am posting it here.

In a Windows console program I use SetClipboardText() to copy short text strings to the clipboard. The first few times this works fine, but after several calls to SetClipboardText the program reproducibly gives wrong results, and finally crashes.

(This only happens with the bound executable, not if I run the program from the Euphoria interpreter.)

Unlikely as this seems, the only explanation I have found for the program's behavior is that data in memory seems to get overwritten by SetClipboardText().

I have no knowledge neither of C nor of the inner workings of Windows nor Euphoria, so I do not know how to pinpoint the problem, let alone do any debugging.

Windows XP, Euphoria 3.1.1, conclip.ew 0.4.

The program where this problem occurs is a command line calculator called Hypatia http://www.drs.at/hypatia

For any help I would be grateful!

Robert Schächter

did you use bindw with -con option?

new topic     » goto parent     » topic index » view message » categorize

4. Re: Problem with copying text to the Windows clipboard

Yes, I have used -con

This is an example of getting a wrong result:

copyall 
 
27 1.28 * 
= 34.56 
 
30 % 
= 10.368 
 
6 1.28 * 2 rd 
= 7.68 
 
34.56 10.37 - 7.68 + 
= 48.38 (this is wrong) 
 
34.56 10.37 - 7.68 + 
= 31.87 (the same calculation, now it is correct) 
 
and now some random input, until it crashes: 
 
1 rd 
 
61.28 + 61.28 - 
 
173.27 28.999 * 61.28 - 
 
2 rd 
 
34.56 10.37 - 7.68 + 
 
235.78 246.77 31.004 33.889 sum 
 
52328.22 7474 + 22 - sqrt 2 rd 30 % 
 
27 1.28 * 
 
30 % 
 
6 1.28 * 2 rd 
 
34.56 10.37 - 7.68 + 
 
and now the crash: 
 
C:\EUPHORIA\include\get.e:135 in function get_number()  
A machine-level exception occurred during execution of this statement  
    <no-name> = 1 
    <no-name> = <no value> 
    <no-name> = 3 
    <no-name> = <no value> 
    <no-name> = 7.6 
    <no-name> = 100'd' 
    <no-name> = <no value> 
    <no-name> = <no value> 
 
... called from C:\EUPHORIA\include\get.e:240 in function Get()   
    <no-name> = <no value> 
    <no-name> = <no value> 
 
... called from C:\EUPHORIA\include\get.e:302 in function value()   
    <no-name> = {55'7',46'.',54'6',56'8',57'9'} 
 
... called from .\hy-lib.e:280 in function isindeednumber()   
    <no-name> = {55'7',46'.',54'6',56'8'} 
    <no-name> = {0,7.68} 
    <no-name> = 7.68 
 
... called from .\hy-calc.e:12 in procedure makenumeric()   
    <no-name> = {55'7',46'.',54'6',56'8'} 
    <no-name> = {0,7.68} 
    <no-name> = <no value> 
    <no-name> = 4 
    <no-name> = <no value> 
    <no-name> = <no value> 
 
... called from hy.exw:108 in procedure processline()   
    <no-name> = <no value> 
    <no-name> = 0 
    <no-name> = <no value> 
 
... called from hy.exw:1  

I was wrong in one regard: Meanwhile I've experienced a crash with running it from the interpreter, too.

BTW, I use a shortened and slightly modified version of conclip, but I've tested it with the original, with the same result.

I'm still open to the possibility that it can be a logical bug in my program after all, of course.

So, my main question: is it indeed conceivable, that the clipboard routine corrupts data in memory? And is there a way to find out whether it does?

Thank you for all your efforts!

Robert

new topic     » goto parent     » topic index » view message » categorize

5. Re: Problem with copying text to the Windows clipboard

Robert:
I am not an expert on using the Clipboard or that particular
clipboard code but I have a few ideas.

The global allocate function was originally used by MS for 16-bit
applications running in win32 and allocates memory from the
internal Heap. Euphoria 3.11 also uses the same Heap for its
memory management. Ver 4.0 has changed the way it handles
memory management on Win XP. Therefore you might consider
trying to use Ver 4.0 for your application and see if you
experience the same problem.

The other thing you could try is to open an edit window in XP.
Open your calculator program and paste some notation to
and from the edit window and see if that works ok.
If does not work then it could be something in the calculator
code.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Problem with copying text to the Windows clipboard

bernie said...

The global allocate function was originally used by MS for 16-bit
applications running in win32 and allocates memory from the
internal Heap. Euphoria 3.11 also uses the same Heap for its
memory management. Ver 4.0 has changed the way it handles
memory management on Win XP. Therefore you might consider
trying to use Ver 4.0 for your application and see if you
experience the same problem.

Actually, they both ultimately use the same allocation function on windows. The difference is that 3.1 also caches the memory internally, in an attempt to speed up allocation and freeing memory. In effect, this duplicates the work of the OS. In the early days, this method was much faster, but as operating systems have matured, the difference has disappeared or shrunk.

There are other issues, where older versions of windows do not guarantee 8-byte aligned allocations (Win2K+ does, IIRC, as well as modern linuxes and bsds). This is critical for euphoria objects, and the internal memory handling has some workarounds to be able to properly allocate and free memory when the OS does not guarantee this.

Also, the clip board requires memory to be allocated in a special way, using GlobalAlloc, which I believe Greg's library uses. I haven't looked at the code in question, though, so I can't say where the problem might be (and I'm not on windows now, so I can't test it).

Matt

new topic     » goto parent     » topic index » view message » categorize

7. Re: Problem with copying text to the Windows clipboard

@Bernie, Clarification about GloblaAlloc() Modern OS works in protected mode. This means that the OS allocate memory space for each application and other applications can't access that memory. Euphoria heap is in application memory space and that can't be accessed by the clipboard. This is why GlobalAlloc() is used. This function allocate memory on system wide heap. That is avalaible to all applications. So that the clipboard can access this data and copy it in its own memory space. I didn't study the euphoria source code but don't think it use GlobalAlloc() when a user use allocate(). If so the code should be corrected, this a major bug.

new topic     » goto parent     » topic index » view message » categorize

8. Re: Problem with copying text to the Windows clipboard

jacques said...

@Bernie, Clarification about GloblaAlloc() Modern OS works in protected mode. This means that the OS allocate memory space for each application and other applications can't access that memory. Euphoria heap is in application memory space and that can't be accessed by the clipboard. This is why GlobalAlloc() is used. This function allocate memory on system wide heap. That is avalaible to all applications. So that the clipboard can access this data and copy it in its own memory space. I didn't study the euphoria source code but don't think it use GlobalAlloc() when a user use allocate(). If so the code should be corrected, this a major bug.

This wrong, I should have checked before writing. http://msdn.microsoft.com/en-us/library/aa366596(VS.85).aspx

new topic     » goto parent     » topic index » view message » categorize

9. Re: Problem with copying text to the Windows clipboard

Thank you! I'll try with 4.0, but this may take a day or two until I've set it up and got acquainted with it.

I cannot paste to and from an editor, my program (so far) can only write to the clipboard SetClipboardText()is the only routine in conclip.e that I call.

BTW, one thing I forgot to mention: with an earlier version of conclip, the program crashed reliably after the (I think) 7th call to SetClipboardText(); with the recent version (0.4, from September 2008) it takes much longer at first I had thought the problem was already solved, until it showed up again.

Robert

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu