Setting an object to NIL from c code

101 views
Skip to first unread message

Francesco Perillo

unread,
Jun 19, 2026, 10:45:29 AM (5 days ago) Jun 19
to harbou...@googlegroups.com
Imagine in a .prg you have:

LOCAL oVar

oVar := myObject():New()
// use oVar, that has complex c internals
// that can't be handled by normal harbour GC
forceDelete( oVar )  <---- here I want to set oVar to NIL
oVar:method()  <--- here it must fail !

In forceDelete I do everythink I need to do and I want to set oVal to NIL so that any further reference to oVar whould be an error !

I did try:
   PHB_ITEM pObject = hb_param( 1, HB_IT_OBJECT );
   // do my stuff
   hb_itemPutNil( pObject );

but it doesn't work...

hmpaquito

unread,
Jun 19, 2026, 10:58:00 AM (5 days ago) Jun 19
to Harbour Developers
So perhaps

forcedelete(@oVar)

FUNCTION forcedelete(oVar)
oVar:= NIL
RETURN NIL

Raphael

unread,
Jun 19, 2026, 11:03:54 AM (4 days ago) Jun 19
to harbou...@googlegroups.com
And if forceDelete() needs to be in C you can do something like:

HB_FUN(FORCEDELETE)
{
    PHB_ITEM pObject = hb_param(1, HB_IT_BYREF);

    if (pObject && HB_IS_OBJECT(pObject)) {
        // do my stuff
        hb_stor(1);
    }
}

You have to call it as forceDelete(@oVar) though.

--
You received this message because you are subscribed to the Google Groups "Harbour Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-deve...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-devel/d040cf48-6300-4577-8086-32325668297en%40googlegroups.com.

Raphael

unread,
Jun 19, 2026, 11:08:45 AM (4 days ago) Jun 19
to harbou...@googlegroups.com
One more thing: I think you can use hb_itemClear(), hb_itemSetNil() or even hb_itemPutNil() instead of hb_stor() so you don't repeat the parameter number (I didn't test it).

Hurricane

unread,
Jun 19, 2026, 2:23:17 PM (4 days ago) Jun 19
to harbou...@googlegroups.com

Easy. For objects created in C, I use:
oVar:=NIL // You don't need to invent anything

I don't see any point in that (unnecessary):
forceDelete( oVar )  <---- here I want to set oVar to NIL

In a more robust environment:
freeAndNil( @oVar ) // or 
FREEANDNIL oVar1, oVar2, oVar3

 I have a basic function with extra instructions:
function freeAndNil( obj )
   .... //  conditioned logic
   .... //  conditioned logic
   .... //  conditioned logic
   obj:=NIL
RETURN NIL


Regards,
Hurricane


Francesco Perillo

unread,
Jun 19, 2026, 3:32:49 PM (4 days ago) Jun 19
to harbou...@googlegroups.com
Thank you everybody,
actually calling forcedelete( @oVar ) solves the problem.

But I tought that objects are always passed as references... they are not...

I'd like to find a way to not use @... if possible... or that supports both methods...

This is the code I used as a test
#include "hbclass.ch"
procedure main
local o
o := myClass():New()
o:list()
forcedelete( @o )
? "Valtype=", valtype(o)

class myClass
    DATA data1
    METHOD list()
end class

method list() class myClass
  ? "I was here !"
return

#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>

HB_FUNC( FORCEDELETE )
{

 PHB_ITEM pObject = hb_param( 1, HB_IT_OBJECT );
//   // do my stuff
 hb_itemPutNil( pObject ); // doesn't work
}
#pragma ENDDUMP


Francesco Perillo

unread,
Jun 19, 2026, 4:32:21 PM (4 days ago) Jun 19
to harbou...@googlegroups.com
Weeks ago someone posted a link to  a file with all the harbour documentation, a file that should be fed to a AI engine.
It was a nice summary of c interface.
Does anybody have a link?

Eric Lendvai

unread,
Jun 19, 2026, 6:35:58 PM (4 days ago) Jun 19
to harbou...@googlegroups.com

Bacco

unread,
Jun 19, 2026, 6:37:38 PM (4 days ago) Jun 19
to harbou...@googlegroups.com
I always struggle to use the C interface by the lack of documentation, perhaps the devs involved with the deep aspects can help us with basic principles related to proper memory management, perhaps some samples of correct ways to do things.

I usually get into my goals in C and C++, but always depending to study already existing code on the main repo and contribs.

Some of my main doubts are:
- the proper use of the garbage collector, when to use, when not
- proper way to use string copy on write mechanisms
- how to implement compile time features (like, how to implement a function that resolves at compile time when used with constant values)
- the difference of the various type conversions between harbour values and C values (in special the variants of string ones)

There are a lot of this issues, if the main/experienced devs can help, we can perhaps list such kinds of scenarios to produce some folder with proper references/samples with proper comments detailing the caveats/reasons of each characteristic








Francesco Perillo

unread,
Jun 20, 2026, 2:12:28 AM (4 days ago) Jun 20
to harbou...@googlegroups.com
Hi Bacco,
at the link Eric sent (thank you!!!) you will find a concise but really interesting course on interfacing harbour and c.

It is a starting point and the samples are really interesting.

I agree with you that there should be an advanced book with more in depth informations on the stack VM and how to debug it. 

For example I'm now trying to investigate a random crash in hbQt. The crash is in qt code.
The program doesn't crash if before calling qt I create and delete an object... this act probably zeroes some memory, clears the stack or ... or I think it is deleted from vm stack but it is not...



Bacco

unread,
Jun 20, 2026, 10:13:20 AM (4 days ago) Jun 20
to harbou...@googlegroups.com
Hi Francesco

The first test I use to do when HbQt crashes is to create an
intermediate object 

e.g.: instead of QSomething(QAnotherthing()) using
oVar := QAnotherthing()
QSomething(oVar)

(not a real solution, but a way to locate the problem)
"Use after free" is a very common problem in hbQt.

Is your problem related to something like that?



Francesco Perillo

unread,
Jun 20, 2026, 11:00:14 AM (4 days ago) Jun 20
to harbou...@googlegroups.com
The answer to your question is .... *probably*

In the last week I forked hbQt and added tons of hb_trace, following the runtime of a small sample program almost line by line.

As you know qt qobjects have their own life and in hbQt we decided to keep this behavior: you may have a living qt object without a living harbour variable, like in cpp. But it brings a lot of situations that may be not easily addressable and need helpers classes both in harbour and in cpp. Some of these cases are also present if we decided that there should a living harbour variable for each qt object.

The problem I found and solved in my branch were related to the codeblocks used with signals and events: they were not, in my opinion, correctly released, and gc, due to variables present in those codeblocks,  could not fully release. 

But despite these specific and other changes I still get a crash opening a QDialog for the nth time, with n>1.
Using the debugger, the crash happens in qt code, in qdialog:exec() where a qscopedpointer fails, in a call stack of screen painting, asking the objects for accessible (for blind people f.e.) features.

Two ways to not have the crash:
- keep the qt object live but hidden
- create an hbqt object aka do something... on the vm stack, on hbqt internals?

I created a qwidget, as per the infamous 
:setparent(qwidget())
and now I wonder if the setparent or the qwidget() creation solved, well, masked the problem.

Probably, somewhere inside hbQt, there is code that mishandle a PHB_ITEM expecially for objects... f.e. in which cases should I do a hb_arrayId?

Francesco

Bacco

unread,
Jun 20, 2026, 11:16:53 AM (3 days ago) Jun 20
to harbou...@googlegroups.com
About the QDialog part, have you checked if Qt::WA_DeleteOnClose flag changes the behaviour?

Francesco Perillo

unread,
Jun 20, 2026, 11:39:21 AM (3 days ago) Jun 20
to harbou...@googlegroups.com
Yes i did.
I forwarded a couple of messages to you.
In the first one you can read the quoted messages to have some history

The second, i deleted the quoted part.

The branch is in the github if you want to have a look, and I'm using hbqtwidget/tests/comp2.hbp as a test.

The crash is from qt5.6+



Reply all
Reply to author
Forward
0 new messages